How to Show all elements of an array declared in a C struct?

Asked

Viewed 1,917 times

0

In that code:

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

struct cadastro {
   char nome[50];
   int idade;
   char rua[50];
   int numero;

   };
int main()
{
struct cadastro c[4]; //Array[4] de estruturas
for(int i=0; i<4; i++) {
    gets(c[i].nome);
    scanf("%d", &c[i].idade);
    gets(c[i].rua);
    scanf("%d", &c[i].numero);
}
 system("pause");
 return 0;
}

How to print all the elements of the struct, without doing it manually with a printf. For he will exhibit four times.

  • I think the title is poorly formulated.

  • Tip 1: Do not use gets(); it is impossible to use this function safely; the function was removed from the C Standard in 2011; replaced by fgets(). Suggestion 2: Do not mix fgets() (gets()) with scanf(); always use fgets() and, if necessary, sscanf().

  • @pmg I’m reading the C99 version.

2 answers

0

Hello, I think it’s unclear what you meant to ask.

First, there is no method more recommended for output values in C than with printf.

To print the names, just follow the same logic you used to read them:

for (i=0;i<4;i++){
    printf("%s\n",c[i].nome);
}
  • Declaring a function would not be better?....

  • The algorithm to be used regardless of whether you are inside a function or not. That’s up to you.

0


To print all elements at once, with a single simple instruction, use 1 function!

void cadastro_print(struct cadastro *p) {
    printf("nome: %s\n", p->nome);
    printf("idade: %d\n", p->idade);
    printf("rua: %s\n", p->rua);
    printf("numero: %d\n", p->numero);
    printf("\n");
}

and, in main(), calls this function

    for (int i = 0; i < 4; i++) cadastro_print(c + i);
  • I had to put --Std=C11 in my compiler

  • The problem of reading is the mixture of gets() with scanf(). The scanf() leaves ENTER "hanging" and gets() next takes the empty line.

  • Why does fgets read with n? , right? http://pastebin.com/DnPnADBV

  • But there’s an error in this, it doesn’t print correctly, it doesn’t read the variables correctly.

  • I made a version with fgets() and excluding ENTER final: http://ideone.com/tNkSdH

  • But what about the C99 version of ideone? , okay.

  • Passes the definition of variable of the for out of the cycle, to the beginning of the block, and the program becomes C89 for (int i = 0; ...) passes to int i; for (i = 0; ...)

  • But programming in older versions of C, isn’t it worse? , declaring variable i(int) before is considered bad?

  • There are still compilers that have not been upgraded to C99, let alone C11. If you have to use one of these compilers, you have to use C89. In addition some of the novelties of the new versions are almost only "syntactic sugar" :)

  • I am using gcc Compiler, and codeblocks. but there is a way to define build options.

  • gcc has parameters to define the language: gcc -std=c89 -pedantic ... or gcc -std=c99 -pedantic ... or gcc -std=c11 -pedantic ...; I don’t know the codeblocks.

  • Yes, I defined --Std=C11 in compiler, from codeblocks, because before I did not accept to declare the variable in for.

  • Thank you, now it’s working properly.

Show 8 more comments

Browser other questions tagged

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