[英]How to properly initialise a struct in which one of the members is an array?
我定义了以下结构:
typedef struct sp_point_t* SPPoint;
struct sp_point_t
{
int dim;
int index;
double* data;
};
然后,我想初始化该结构的实例:
foo (double* data, int dim, int index)
{
double* dataInserted;
dataInserted = (double*) calloc(dim,sizeof(double));
//inserting values to dataInserted
SPPoint newPoint = {dim, index, dataInserted}; // warning!
}
但是在编译时,我遇到了“标量初始化程序中的多余元素”的问题(在最后一行)。
此警告是什么意思? 为什么不能以这种方式初始化实例?
谢谢
您正在初始化指向struct的指针,而不是stuct本身。 以下工作(如果您要在代码中创建结构):
foo (double* data, int dim, int index)
{
double* dataInserted;
dataInserted = (double*) calloc(dim,sizeof(double));
//inserting values to dataInserted
struct sp_point_t newPoint = {dim, index, dataInserted}; // Corrected code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.