C examples for Structure:Structure Definition
A struct is a value type, not a reference type.
Any assignment or argument passing for struct object will copy the field values.
typedef struct point point; struct point {/*ww w. j av a2 s.c om*/ int x, y; } r = { 1, 2 }; /* assigns x and y */ int main(void) { point p = { 1, 2 }; point r = p; /* copies field values */ }
It is common to use pointers when passing objects to functions.
typedef struct point point; struct point {/*from w ww. ja v a 2 s . c om*/ int x, y; } r = { 1, 2 }; /* assigns x and y */ void init_struct(point* a) { (*a).x = 1; (*a).y = 2; } int main(void) { point p; init_struct(&p); }
To reference struct field via struct pointer, use the infix operator -> which automatically dereferences the pointer.
point p;
point* r = &p;
r->x = 1; /* same as (*r).x = 1; */