指向全局嵌套结构的函数
我在编写一个将指针分配给全局嵌套结构地址的函数时遇到问题。 但我希望在函数内部完成,而不是在 main 中完成 请帮我写这个函数。在此先致谢
#include "stdio.h"
typedef struct
{
int c;
}code;
typedef code* p_code ;
typedef struct
{
char a;
int b;
code krish;
}emp;
emp Global_Sam;
int main()
{
code tmpcode_krish;
code* pcode_krish;
pcode_krish = &tmpcode_krish;
printf("Goal %p %p \r\n ", &(Global_Sam.krish), &(Global_Sam).krish);
memset(pcode_krish, 0 , sizeof( code));
// pcode_krish = &Global_Sam.krish;
PointNestedStructToPointer(&pcode_krish);
printf("Goal=> both should be same => %p %p \r\n ", &(Global_Sam.krish), pcode_krish);
return 0;
}
, => pcode_krish = &Global_Sam.krish;
这将指向全局嵌套结构。但我需要在函数内部执行此操作,因此函数 PointNestedStructToPointer
void PointNestedStructToPointer(p_code *dst )
{
dst = &Global_Sam.krish;
}
上面的函数并不反映全局嵌套结构的确切地址,我已将打印进行了验证。请帮忙
I am having trouble in writing a function which assigns pointer to the address of global nested structure.
But i would like that to be done inside a function, not with in main
Please help me in writing the function. Thanks in advance
#include "stdio.h"
typedef struct
{
int c;
}code;
typedef code* p_code ;
typedef struct
{
char a;
int b;
code krish;
}emp;
emp Global_Sam;
int main()
{
code tmpcode_krish;
code* pcode_krish;
pcode_krish = &tmpcode_krish;
printf("Goal %p %p \r\n ", &(Global_Sam.krish), &(Global_Sam).krish);
memset(pcode_krish, 0 , sizeof( code));
// pcode_krish = &Global_Sam.krish;
PointNestedStructToPointer(&pcode_krish);
printf("Goal=> both should be same => %p %p \r\n ", &(Global_Sam.krish), pcode_krish);
return 0;
}
Here,
=> pcode_krish = &Global_Sam.krish;
this will point to the global nested structure. But i need to do that inside a function, hence the function, PointNestedStructToPointer
void PointNestedStructToPointer(p_code *dst )
{
dst = &Global_Sam.krish;
}
The above function doesn't reflect the exact address of the global nested structure, i have put prints a verified. Please help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要相当于“
在函数内部”的功能,您有两个选择:
将指针的地址传递给函数:
从函数返回指针并自己进行赋值:
If you want the equivalent of
Inside a function you have two options:
Pass the address of a pointer to the function:
Return the pointer from the function and do the assignment yourself:
在
PointNestedStructToPointer()
中进行赋值时取消引用dst
:Dereference
dst
when making the assignment inPointNestedStructToPointer()
: