Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C传递嵌套结构作为引用_C_Struct_Reference - Fatal编程技术网

C传递嵌套结构作为引用

C传递嵌套结构作为引用,c,struct,reference,C,Struct,Reference,我试图在不同的结构中定义不同的特性。但程序既不工作也不出错。我希望打印机函数打印我定义的值。但在控制台屏幕上什么也不给 #include <stdio.h> #include <stdlib.h> typedef struct { int x; int y; int z; }points; typedef struct { points *dimensions; int ID; char *name; }featu

我试图在不同的结构中定义不同的特性。但程序既不工作也不出错。我希望打印机函数打印我定义的值。但在控制台屏幕上什么也不给

#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()
{
    features *ball;
    ball->dimensions->x = 10;
    ball->dimensions->y = -5;
    ball->dimensions->z = 15;
    ball->ID           = 121121;
    ball->name         = "redball";

    printer(ball);
return 0;
}
#包括
#包括
类型定义结构
{
int x;
int-y;
intz;
}点数;
类型定义结构
{
点*尺寸;
int-ID;
字符*名称;
}特征;
无效打印机(功能*测试)
{
printf(“尺寸=[%d%d%d]\r\n”,测试->尺寸->x,测试->尺寸->y,测试->尺寸->z);
printf(“ID=%d,name=%s\r\n”,test->ID,test->name);
}
int main()
{
特点*球;
球->尺寸->x=10;
球->尺寸->y=-5;
球->尺寸->z=15;
球->ID=121121;
ball->name=“redball”;
打印机(球);
返回0;
}

我确信现在我看不出有一点小错误。

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;
}

还定义“不工作”。
警告:此函数中未初始化时使用“ball”[-Wuninitialized]
-指针必须指向某个对象。@fredrarson是的,我明白了。但是ball被定义为相同的类型。
ball
是一个指针,但它指向哪里?
ball
定义明确,但未初始化。它没有指向任何东西,所以它指向谁知道在哪里。谢谢!现在我明白了为什么它不提供任何输出。
dimensions = [ 10 -5 15 ]
ID = 121121, name = redball