ball
и dimensions
- это просто указатели . Но они оба указывают в никуда.
Вам необходимо выделить память, куда они должны указывать для обоих, например, используя malloc()
, и назначить адрес памяти указателям:
features *ball = malloc (sizeof(features)); // Memory for structure features allocated and assigned to pointer.
ball->dimensions = malloc (sizeof(points)); // Memory for structure points allocated and assigned to pointer.
В вашей программе:
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
int y;
int z;
} points;
typedef struct
{
points *dimensions;
int ID;
char *name;
} features;
void printer (features *test)
{
printf("dimensions = [ %d %d %d ]\r\n", test->dimensions->x, test->dimensions->y, test->dimensions->z);
printf("ID = %d, name = %s\r\n", test->ID, test->name);
}
int main (void)
{
features *ball = malloc (sizeof(features)); // Memory for structure features allocated.
ball->dimensions = malloc (sizeof(points)); // Memory for structure points allocated.
ball->dimensions->x = 10;
ball->dimensions->y = -5;
ball->dimensions->z = 15;
ball->ID = 121121;
ball->name = "redball";
printer(ball);
return 0;
}
Вывод:
dimensions = [ 10 -5 15 ]
ID = 121121, name = redball