Kvixen
Kvixen

Reputation: 47

Struct in header file produces error in source file

I have a struct defined inside capi_utils.h that looks like the following:

#ifndef _CAPI_UTILS_H_
# define _CAPI_UTILS_H_

...

struct ScalarVariable{
    char name[63];
    float value;
    uint8_T DataID;
    char type[50];
};

...
#endif

Inside of capi_utils.c I tried creating a variable to hold the struct like this

struct ScalarVariable sVariable;

Which only produces an error when I try setting a value inside the struct like this:

sVariable.name = paramName;

Error message is:

capi_utils.c:27: error: invalid use of undefined type `struct ScalarVariable'

What am I doing wrong?

EDIT 1:

I just had to include capi_utils.h. Didn't think I had to because I had understood source files and headers different for some reason.

EDIT 2:

To clarify, I even got errors when trying to set DataID, not only the array.

void GetValueFromAdress(const char_T*  paramName,
                     void*          paramAddress,
                     uint8_T        slDataID,
                     unsigned short isComplex,
                     uint_T*        actualDims,
                     uint_T         numDims,
                     real_T         slope,
                     real_T         bias) {

sVariable.DataID = slDataID;

}

Would produce error: invalid use of undefined type 'struct ScalarVariable'

Upvotes: 1

Views: 69

Answers (1)

gsamaras
gsamaras

Reputation: 73366

In cape_utlis.c, you need to include the header file, like this:

#include cape_utlis.h

Moreover, change this:

sVariable.name = paramName;

to this:

strcpy(sVariable.name, paramName)

in order to copy the NULL-terminated string in C, you use the function strcpy, and not the assignment operator.

Upvotes: 1

Related Questions