gpt4 book ai didi

c - 结构体指针作为参数并返回

转载 作者:行者123 更新时间:2023-11-30 19:20:49 27 4
gpt4 key购买 nike

我有一个函数,它接受一个结构指针作为参数并返回它。在该函数中,我希望另一个函数填充指针指向的内存。我的代码是

struct my_struct{
unsigned char** ps;
unsigned long* pl;
};

struct* function(struct* param){

another_func(param->ps,param->pl)//function takes pointers as parameters and fills them up
return param;
}

int main{
my_struct *p;
p=function(p);
}

//definiton of another func is;

void another_func(unsigned char**,unsigned long * ){...}

编辑:它给出了错误访问冲突

最佳答案

根据您迄今为止发布的内容,尝试改为:

typedef struct my_struct 
{
unsigned char** ps;
unsigned long* pl;
} my_struct;

void another_func(unsigned char**,unsigned long * ) {...}

my_struct* function(my_struct* param)
{
another_func(param->ps,param->pl)
return param;
}

int main()
{
my_struct *p;
my_struct q = {NULL,NULL};
unsigned long pl = 10;
q.ps = malloc( pl * sizeof(char*) );
q.pl = &pl;
p=function(&q);
return 0;
}

聊天后编辑

关于c - 结构体指针作为参数并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21180882/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com