Search code examples
cpointersstructureincompatibletypeerror

Incompatible types when initializing type error


I'm facing problems with pointers and structures in C. Let's say:

typedef struct mdata{
   int a;
   int b;
    ...
} pred_data_attr;

typedef struct mConfig{
   pred_data_attr mdata;
   ...
} mConfig;

struct control{
   struct mConfig mcfg;
   ...
};

INT32 rdfapp(struct control *rdf){
    pred_data_attr* mdata = rdf->mcfg.mdata;
    ...
}

I got this error:

incompatible types when initializing type 'struct pred_data_attr *' using type 'pred_data_attr'

How can I fix this problem?


Solution

  • What about:

    INT32 rdfapp(struct control *rdf)
    {
      pred_data_attr * mdata = &rdf->mcfg.mdata;
    

    The namings in your code are another good example how inappropriate naming can lead to irritations and errors.

    It is not a good idea to name different things the same:

    pred_data_attr * mdata /* here mdata is a pointer */
    
    pred_data_attr mdata; /* here mdata is the instance of a structure */
    

    There are plenty of possiblilites how naming can be setup, from which I do not want to recommend any here as it's primarily about:

    Give different names to different things.