[英]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.