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

标签 c pointers struct

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

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/

相关文章:

java - 这里如何避免使用全局变量?

康威生命游戏函数中的逻辑错误

c++ - 为什么不允许在其元素中使用结构变量本身?

c - 使用 sizeof 作为指向 2 个 float 数组的指针

C 为什么 void 指针不能存储浮点值?

c++ - 结构中的字段跳过字节

c - C 中的指针和数组引用

c++ - munmap_chunk() : invalid pointer: 0x00007fffbef49d90 in a recursive function

c - 在 C 结构中初始化矩阵变量

c++ - Arduino 结构变量似乎在没有明确重新分配的情况下被重新分配