dynamic matrix allocation within a struct

Asked

Viewed 394 times

0

statement and code:

Make a program that stores movies produced by various directors and: - Create and read a vector of 5 directors, each containing name (maximum 20 letters), number of movies and movies. The member films is a vector, which must be created after having read the amount of movies. Each film is composed by name, year and duration. - Find a director by name, showing all the films he has ever produced. Repeat the process until you enter an empty string.

#define QTD 2

typedef struct diretor {
    char nome[20], **filme;
    int qtd;
} DIRETOR;

int main() {
    setlocale(LC_ALL,"portuguese");

    DIRETOR diretor[QTD];
    char busca[20];

    for (int i=0; i<QTD; i++) {
        printf("Nome do diretor: ");
        fflush(stdin);
        fgets(diretor[i].nome, 20, stdin);
        diretor[i].nome[strlen(diretor[i].nome) -1] = '\0';

        printf("Quantidade de filmes: ");
        fflush(stdin);
        scanf("%d", &diretor[i].qtd);

        while (isalpha(diretor[i].qtd) != 0) {
           printf("Entre com um número válido.\n");
           printf("Quantidade de filmes: ");
            fflush(stdin);
            scanf("%d", &diretor[i].qtd);
       }

       diretor[i].filme = (char **) malloc(sizeof(DIRETOR) * diretor[i].qtd);
        for (int l=0; l<QTD; l++) diretor[i].filme[i] = (char*) malloc (20 * sizeof(DIRETOR));

        for (int j=0; j<diretor[i].qtd; j++) {
            printf("Filme %d:", j+1);
            fflush(stdin);
            fgets(diretor[i].filme[j], 20, stdin);
            diretor[i].filme[j][strlen(diretor[i].filme[j]) -1] = '\0';
        }
        printf("\n");
    }

    do {
        printf("Diretor que deseja buscar: ");
        fflush(stdin);
        fgets(busca, 20, stdin);
        busca[strlen(busca) -1] = '\0';

        for (int i=0; i<QTD; i++) {
            if (strcmpi(busca, diretor[i].nome) == 0 && strcmp(busca , "") != 0) {
                printf("Filmes:\n");
                for(int j=0; j<diretor[i].qtd; j++)
                    printf("%s\n", diretor[i].filme[j]);
            }
        }
    } while (strcmp(busca , "") != 0) ;
}

my doubt is: I’m allocating the matrix correctly?

because it doesn’t work when I try to put more than 2 movies in the same director! from now on, thank you

1 answer

2


my doubt is: I’m allocating the matrix correctly?

Not really. The allocation was made based on the size of DIRETOR, allocating a vector of DIRETORES but the kind of filme is char**:

diretor[i].filme = (char **) malloc(sizeof(DIRETOR) * diretor[i].qtd);
//                       ^---- tipo            ^---- tamanho alocado de cada elemento

That is to say, it allocated a vector of directors and stored it in a double char pointer, and so the types and sizes do not play. If your matrix is a matrix of char** to store strings stated in the form of char **filme then the correct allocation would be:

diretor[i].filme = malloc(sizeof(char *) * diretor[i].qtd);
//                                  ^----

Note that I omitted the cast because it is not mandatory. I must also add that call to the field filmes would be much clearer than it represents, rather than filme.

But I cannot help but say that this does not respond to the statement. The statement clearly states that:

Each film is composed by name, year and duration

So filme should be a structure:

typedef struct filme {
   char nome[50];
   int ano, duracao;
} FILME;

And now this would be the structure used inside DIRETOR:

typedef struct diretor {
    char nome[20];
    FILME *filmes; // <--- vetor dinâmico de filmes
    int qtd;
} DIRETOR;

Allocation would now be:

diretor[i].filmes = malloc(sizeof(FILME) * diretor[i].qtd);

Now we have to adjust the code that remains for this new film structure.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.