pointer to member of struct [closed]

2019-06-19 04:28发布

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);    

}      

3条回答
The star\"
2楼-- · 2019-06-19 04:32

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

int *ss;
查看更多
在下西门庆
3楼-- · 2019-06-19 04:35

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

查看更多
放我归山
4楼-- · 2019-06-19 04:44

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);
} 
查看更多
登录 后发表回答