-1
Good afternoon guys, I’m learning to use pointers in struct
's... so I tried to do a record exercise of nome
and id
very simple with a vector size for only 3 entries.
I can’t find the error of my code. When I compile, I see memory junk. I need help!!! Thank you! Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id;
char nome[40];
} cadastro;
int main(int argc, char** argv) {
cadastro v[3], *pv; //uma struct v[3] e um ponteiro para v[3] (pv);
int i = 0;
pv = &v[0]; //pv recebe o endereço de v[0];
while (i < 3) {
printf("Digite o id:");
scanf("%d", &pv->id); //pegar o id
printf("Digite o nome:");
scanf("%s", &pv->nome); //pegar o nome
++pv; //passar pro proximo endereço
i++;
}
int j = 0;
while (j < 3) {
printf("Id: %d\n", pv->id);
printf("Nome: %s\n", pv->nome);
++pv; //passar pro proximo endereço
j++;
}
return (EXIT_SUCCESS);
}
Then I must remove the pointer increments?
– Samuel Castro Ribeiro
What modifications should I make?
– Samuel Castro Ribeiro
I was able to solve it with your help friend! All I had to do was reset the pointer to the first vector position before entering the second While!
– Samuel Castro Ribeiro
Glad you made it. Hugs.
– João Kevin