Vector of struct in C

Asked

Viewed 8,780 times

1

I can’t find the error in this code of mine of an exercise. It prints right the first reading, then prints random things. I could not find out if the error is in the reading or in the print.

#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 iterating wrong in reading? How would be right ?

  • Could post what output you are getting?

1 answer

1

I believe the biggest problem in the program is the declaration:

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

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

And in function main(), when you declare:

BANDAS banda[TAM];

In fact, he’s creating a matrix, since the typedef already defines a array.

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

struct BANDAS banda[2][2];

and probably, that’s not what you want the show to do.


If you really intend to create the guy BANDAS as array, the solution is to change the variable declaration banda in main to:

BANDAS banda;

and in function ler(), change the references to array 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 on IDEONE


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

Two other tips:

  1. Only use the typedef where it is really necessary, which is not the case for such programs (simple).
  2. The command __fpurge is not part of the standard language C and should be avoided.
  • In addition to the errors pointed out by Voce, I forgot the *band in reading. If __fpurge should be avoided, how should I clean the buffer? Should I use fgets and use strcspn to eliminate " n" ? Thanks for the great help.

  • @Abraham The gets should be avoided as it does not control the size of the input, there is a high risk of a stack overflow. The best, as you said, is to use the fgets. To clear the input buffer, you can use one looping of the kind: while ((c = getchar()) != '\n' && c != EOF); (c is a variable of the type char)

Browser other questions tagged

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