Vector of struct in C

1

I can not find the error in this code of my exercise. It prints right the first reading, then prints random things. I could not find out if the error is in the reading or the printing.

#include <stdio.h>
#include <stdlib.h>
/*Defina uma estrutura que irá representar bandas de música.
Essa estrutura deve ter o nome da banda, que tipo de música ela toca,
 o número de integrantes e em que posição do ranking essa banda está dentre as suas 5 bandas favoritas.*/
#define TAM 2

 typedef struct
 {
     char nome[20],tipo[15];
     int integrantes,posicao;
 }BANDAS[TAM];

int main()
{

    BANDAS banda[TAM];
    ler (&banda);

    mostrar(banda);


    return 0;
}

void ler (BANDAS *banda)
{
    int i;

    for (i=0;i<TAM;i++){
        printf ("Diga qual o nome da banda: ");
        gets(banda[i]->nome);
    __fpurge(stdin);

        printf ("Tipo de musica: ");
        gets (banda[i]->tipo);
    __fpurge (stdin);

        printf ("Quantos integrantes tem a banda: ");
        scanf ("%d",&banda[i]->integrantes);
     __fpurge (stdin);
        printf ("Posicao no seu top 5: ");
        scanf ("%d",&banda[i]->posicao);
        __fpurge (stdin);
    }
}

void mostrar (BANDAS banda)
{
    int i;
    for (i=0;i<TAM;i++){

    printf ("Nome: %s\n",banda[i].nome);
    printf ("Tipo de musica: %s\n",banda[i].tipo);
    printf ("Numero de integrantes: %d \n",banda[i].integrantes);
    printf ("Posicao no seu TOP 5: %d \n",banda[i].posicao);
    }

}

Am I wrong to read? How would that be right?

    
asked by anonymous 26.12.2016 / 01:16

1 answer

1

I think the biggest problem with the program is the statement:

typedef struct
{
    char nome[20],tipo[15];
    int integrantes,posicao;
} BANDAS[TAM];

In this statement you are creating a type array , with TAM positions.

And in the main () function, when you declare:

BANDAS banda[TAM];

In fact, you are creating an array , since typedef already defines an array .

The translation of this statement by the compiler would look something like (example):

struct BANDAS banda[2][2];

And that's probably not what you want the program to do.

If you really want to create the type BANDAS as array , the solution is to change the declaration of the variable banda in main to:

BANDAS banda;

and in the ler() function, change the references to array to, for example:

...
printf("Diga qual o nome da banda: ");
gets((*banda)[i].nome);
...

Below is a functional version of the program with these changes:

#include <stdio.h>
#include <stdlib.h>

#define TAM 2

typedef struct
{
    char nome[20];
    char tipo[15];
    int integrantes,posicao;
} BANDAS[TAM];

void ler (BANDAS *banda);
void mostrar (BANDAS banda);

int main()
{
    BANDAS banda;

    ler (&banda);
    mostrar(banda);
    getchar();
    return 0;
}

void ler (BANDAS *banda)
{
    int i;
    for (i=0; i<TAM; i++)
    {
        printf("Diga qual o nome da banda: ");
        gets((*banda)[i].nome);
        printf("Tipo de musica: ");
        gets ((*banda)[i].tipo);
        printf("Quantos integrantes tem a banda: ");
        scanf("%d",&(*banda)[i].integrantes);    
        printf("Posicao no seu top 5: ");
        scanf("%d",&(*banda)[i].posicao);    
        getchar();
    }    
}

void mostrar (BANDAS banda)
{
    int i;
    for (i=0; i<TAM; i++)
    {
        printf ("Nome: %s\n",banda[i].nome);
        printf ("Tipo de musica: %s\n",banda[i].tipo);
        printf ("Numero de integrantes: %d \n",banda[i].integrantes);
        printf ("Posicao no seu TOP 5: %d \n",banda[i].posicao);
    }
}

See working in IDEONE

Although the program works in this way, you can make it more readable by changing the struct declaration to store only the data of a band, and create array inside the main () function.

Two other tips:

  • Only use typedef where it is really needed, which is not the case for this kind of (simple) programs.
  • The __fpurge command is not part of the C language pattern and should be avoided.
  • 26.12.2016 / 15:59