pointer to member of struct [closed]

2019-06-19 04:04发布

问题:

I am trying to write a C program. I need the address of variable "recq". Can someone pls help me figure that out?

typedef struct {  
    int recq;  
} dd;  


struct test {  
    dd a;  
};

main(){  
    struct test *mm;  
    mm=(struct test *) malloc (sizeof (struct test));    
    ss=&(mm->a.recq);    
    printf("%p",ss);    

}      

回答1:

What you have looks good except you need to declare the ss variable:

int *ss;


回答2:

Your required program is,

#include<stdio.h>

typedef struct {
    int recq;  
} dd;  

struct test {  
    dd a;  
};

void main(void){
    struct test mm;
    printf("%p", &mm.a.recq);
} 


回答3:

First of all, you need to declare ss as "int * " , or use cast whatever the rest of your code is right, I think.