I compiled the code below on gcc(x64).
Why can I access the structure items by putting dot(people[0].name) rather than '->'?
Isn't people[0] is pointing to the address of the structure and I should use people[0]->name or (*people[0]).name to access the name?
Code:
#include <stdio.h>
#define TOTAL 4
int main(void) {
typedef struct tagPerson {
char name[12];
int ID;
} Person;
#define LPPERSON Person*
Person * people = calloc(sizeof(Person), TOTAL);
strncpy(people[0].name, "Henry", 12);
strncpy(people[1].name, "Jacob", 12);
printf("sizeof(people)=%d\n", sizeof(people));
printf("people[0].name=%s\n", people[0].name);
printf("sizeof(people[0])=%d\n", sizeof(people[0]));
printf("people[1].name=%s\n", people[1].name);
printf("sizeof(people[1])=%d\n", sizeof(people[1]));
free(people);
return 0;
}
Output:
sizeof(people)=8
people[0].name=Henry
sizeof(people[0])=16
people[1].name=Jacob
sizeof(people[1])=16
calloc.strncpy()is dangerous. (well, every use of strncpy() is dangerous ...)