指向全局嵌套结构的函数

发布于 2024-10-13 06:55:38 字数 995 浏览 3 评论 0原文

我在编写一个将指针分配给全局嵌套结构地址的函数时遇到问题。 但我希望在函数内部完成,而不是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何止钟意 2024-10-20 06:55:38

如果您想要相当于“

dst = &src;

在函数内部”的功能,您有两个选择:

  1. 指针的地址传递给函数:

    void doit(代码 **p)
    {
        *p = &src;
    }
    
    // 之后
    doit(&pcode_krish);
    
  2. 从函数返回指针并自己进行赋值:

    代码 *doit(void)
    {
        返回&src;
    }
    
    // 之后
    pcode_krish = doit();
    

If you want the equivalent of

dst = &src;

Inside a function you have two options:

  1. Pass the address of a pointer to the function:

    void doit(code **p)
    {
        *p = &src;
    }
    
    // later
    doit(&pcode_krish);
    
  2. Return the pointer from the function and do the assignment yourself:

    code *doit(void)
    {
        return &src;
    }
    
    // later
    pcode_krish = doit();
    
寂寞美少年 2024-10-20 06:55:38

PointNestedStructToPointer() 中进行赋值时取消引用 dst

*dst = &Global_Sam.krish;

Dereference dst when making the assignment in PointNestedStructToPointer():

*dst = &Global_Sam.krish;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文