Reputation: 69
I'm new to structs and pointers and I can't see what's wrong with this code:
struct {
int id;
char* name;
} cap[50];
void xep() {
int i, n;
scanf("%d", &n);
for (i = 0; i < n; i++) {
cap[i].id = i;
scanf("%c", cap[i].name);
printf("%d %s\n", cap[i].id, cap[i].name);
}
}
When calling the xep function in main, it only prints:
0 (null)
1 (null)
2 (null)
Like it ignores everything I input after n. Any ideas?
Upvotes: 0
Views: 47
Reputation: 1660
char* name is a pointer, but you haven't allocated any memory to it. Either give it a fixed size char name[100] or alloc some memory. Your scanf is just getting 1 character, you probably want %s (string) instead of %c (character)
Upvotes: 1