0

I defined the following struct:

typedef struct sp_point_t* SPPoint;

struct sp_point_t
{
    int dim;
    int index;
    double* data;
};

And then I wanted to initialise an instance of the struct:

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

But at compilation time I got an "excess elements in scalar initializer" warring (on the last line).

What does this warning mean? why can't I initialise an instance this way?

Thanks

Gal P
  • 11
  • 1
  • You do not need the cast on `calloc` - see [here](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed Heal May 29 '16 at 18:23
  • 3
    Welcome to Stack Overflow. Please read the [About] page soon, and also the information about creating an MCVE ([MCVE]). You've not shown the typedef for `SPPoint`. Is it `typedef struct sp_point_t SPPoint;` or `typedef struct sp_point_t *SPPoint;` or something else altogether? You've also not shown the local variables `dim` or `index` either. These omissions make it impossible to determine what needs to be done to fix your problem. My guess is that you've got a pointer (in which case, read [Is it a good idea to typedef pointers?](http://stackoverflow.com/questions/750178/)). – Jonathan Leffler May 29 '16 at 18:26
  • `data` is a pointer, not an array – M.M Jun 01 '16 at 07:33

1 Answers1

1

You are initializing a pointer to struct instead of the stuct itself. The following works (if you meant struct creation in your code):

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
}
GMichael
  • 2,726
  • 1
  • 20
  • 30