I'm trying to learn about nested structures and pointers in C. I made this as a test:
typedef struct xyz xyz_t;
struct xyz{
int x, y, z;
};
xyz_t array[] = {
{.x = 14, .y = 16, .z = 18},
{.x = 34, .y = 36, .z = 38},
{.x = 64, .y = 66, .z = 68},
};
typedef struct blue blue_t;
struct blue
{
int *pointer;
};
int main()
{
blue_t red;
red.pointer = &array;
printf("%d",red.pointer[1].z);
}
The idea is to have the structure red have a pointer pointing to array, and then print f.ex. array[1].z.
What am I doing wrong? The compiler is telling me:
assignment from incompatible pointer type
[-Wincompatible-pointer-types]
red.pointer = &array;request for member
xin something not a structure or unionprintf("%d",red.pointer[2].x);
pointerdeclared as pointing at anint?xyz_t, which from that point can be used like any other native type (e.g.int). So a pointer toxyz_tis simplyxyz_t *.&arrayis notxyz_t *, it isxyz_t (*)[3]since it's a pointer to the array and not a pointer to the first element. A pointer to the first element would be&array[0]. Or (since arrays naturally decays to pointers to their first element) plainarray.