___ erkimt __ dynamic array allocation within a struct ______ qstntxt ___

statement and code:

  

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

%pre%

My question is: am I allocating the array correctly?

It does not work when I try to put more than 2 movies in the same director! Thank you.

    
______ azszpr336404 ___
  

My question is: am I allocating the array correctly?

Not at all. The allocation was done based on the %code% size, allocating a %code% vector but the %code% type is %code% :

%pre%

In other words, it has allocated a vector of directors and saved it in a double char pointer, so the types and sizes do not play. If your array is an array of %code% to save strings declared as %code% then the correct allocation would be:

%pre%

Note that I omitted cast because it is not required. I should also add that calling the %code% field would be much brighter than it represents, instead of %code% .

But I can not help saying that this does not answer the statement. The statement clearly states that:

  

Each movie consists of name, year and duration

Then %code% should be a structure:

%pre%

And now this would be the structure used within %code% :

%pre%

The allocation would now be:

%pre%

Now it's not enough to adjust the remaining code for this new movie structure.

    
___

0

statement and code:

  

Make a program that stores movies produced by several directors   and:       - Create and read a vector of 5 directors, each containing name (maximum 20 letters), number of films       and movies. The movie member is a vector, which must be created after reading the number of movies. Each       film is composed of name, year and duration.       - Look for a director by name, showing all the movies he has already 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] = '
#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] = '%pre%';

        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] = '%pre%';
        }
        printf("\n");
    }

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

        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) ;
}
'; 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] = '%pre%'; } printf("\n"); } do { printf("Diretor que deseja buscar: "); fflush(stdin); fgets(busca, 20, stdin); busca[strlen(busca) -1] = '%pre%'; 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 question is: am I allocating the array correctly?

It does not work when I try to put more than 2 movies in the same director! Thank you.

    
asked by anonymous 15.10.2018 / 02:49

1 answer

2
  

My question is: am I allocating the array correctly?

Not at all. The allocation was done based on the DIRETOR size, allocating a DIRETORES vector but the filme type is char** :

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

In other words, it has allocated a vector of directors and saved it in a double char pointer, so the types and sizes do not play. If your array is an array of char** to save strings declared as char **filme then the correct allocation would be:

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

Note that I omitted cast because it is not required. I should also add that calling the filmes field would be much brighter than it represents, instead of filme .

But I can not help saying that this does not answer the statement. The statement clearly states that:

  

Each movie consists of name, year and duration

Then filme should be a structure:

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

And now this would be the structure used within DIRETOR :

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

The allocation would now be:

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

Now it's not enough to adjust the remaining code for this new movie structure.

    
15.10.2018 / 12:22