1

sorry for writing a new question because it gies other questions that are the same but I can not find out what is wrong here :( can anybody help.

My code is

typedef struct First_s
{
    double a;
    double b, c;
    double d;
} First_t;

typedef struct Second_s
{
    char e[10];
    long f;        
    First_t g; 
} Second_t;

int compare(Second_t *x, Second_t *y)
{
    if(x.g.b >y.g.b){
    }
    return 0;
}

but it gives me an Error:

request for member 'g' in something not a structure of union

2 Answers 2

4

You're using . when you should use -> to access the structure fields.

Since x and y are pointers, you cannot access them directly with . unless you first dereference with the asterisk (*) operator, i.e. (*x).g.b works. Nobody does that though, the typical way is to use the -> operator since that's what it's there for.

Also, a compare() function should never modify its arguments, so they should be constant pointers:

int compare(const Second_t *x, const Second_t *y)
{
  /* code here */
}
1
  • thank you, I wanted to give a +1 but my reputation is way below 15 :( May 31, 2014 at 20:51
2

x is a Pointer to a struct. To retrieve the contents of a pointer you need to dereference it first (and check if its not NULL):

int compare(Second_t *x, Second_t *y)
{
    if(x->g.b >y->g.b){
    }
    return 0;
}
1
  • thank you, I wanted to give a +1 but my reputation is way below 15 :( May 31, 2014 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.