c - 检索函数中的指针

标签 c pointers struct variable-assignment dereference

我想要在函数中接收填充数据(包括指针)的结构,我将要填充的结构的指针传递给该函数。现在我意识到退出函数时我会丢失指针,因此我相信我可能需要使用双指针。那是对的吗?我想出了一个示例程序,该程序可以编译,但由于在 main() 中访问无效指针而遇到了段错误(第 43 行),请参见下文:

#include <stdio.h>

#define NUM 100

typedef enum state_e {
        SM_ANALYZE = 0,           
        SM_TX,                    
        SM_PEND,                  
} state_t;

typedef struct data_s {
        int flags;             
        int id;                
        int local_min_tx;      
        int local_min_rx;      
        int local_detect_mult; 
        int egr_obj;           
        int label;             
        int egress_label;      
        int port;              
        state_t state;
}data_t; 

typedef struct content_s{
        state_t state;
        data_t *pData;
        void (*cbck)(data_t*,state_t);
}content_t;

static content_t MyCont[NUM]={0};
static data_t MyDat[NUM]={0};

void callback(data_t *pDat,state_t state);

int main(void)
{
content_t locC = {0};
callback_reg(1,callback);
data_set(1,&MyCont[1],SM_PEND);
data_get(1,&locC);


printf("cbck %p\ndata %p\nstate %d\nlabel %d",locC.cbck,
                                              locC.pData,
                                              locC.state,
                                              locC.pData->label);

MyCont[1].cbck(&MyDat[1],MyDat[1].state);
}

void callback(data_t *pDat,state_t state)
{
        printf("in user callback!\n");
        printf("BFDId %d is now %d\n",pDat->id,state);
        return;
}
int data_set(int id, content_t *pDat, state_t state)                
{                                                                           
        pDat->pData = &MyDat[id];
        pDat->state = state;
}                                                                           

int callback_reg(int id, void (*cb)(data_t*,state_t))        
{                                                                           
    MyCont[id].cbck=cb;
}                                                                           
int data_get(int id, content_t *pDat)
{
    pDat = &MyCont[id];
}
//--------------------------------------------------------------------------

最佳答案

这里

int data_get(int id, content_t *pDat)
{
  pDat = &MyCont[id];
}

本地变量pDat被赋值,然后函数被保留,并且pDat被释放为本地变量。

你想要的是:

#include <errno.h> /* for errno */

/*
 * Gets content_t from MyConf at index id.
 *
 * Returns 0 on success or -1 on error.
 */
int data_get(size_t id, content_t *pDat)
{
  int result = 0;

  if (NULL == pDat)
  {
    errno = EINVAL;
    result = -1;
  }
  else if (NUM <= id) 
  {
    errno = ERANGE;
    result = -1;
  }
  else
  {
    *pDat = MyCont[id]; /* Copy MyCont[id] to where pDat points. */
  }

  return result;
}

并这样调用它:

#include <stdlib.h> /* for EXIT_FAILURE */
#include <stdio.h> /* for perror() */

int data_get(int, content_t *);

int main(void) 
{
  ..
  if (-1 == data_get(1,&locC))
  {
    perror("data_get(1, ...) failed");
    exit(EXIT_FAILUE);:
  }

关于c - 检索函数中的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46136043/

相关文章:

c - 如何修改 detab 以接受参数列表

c - 为什么我从这段代码中得到 "request for member in something not a struct or union"?

c - 确定原始退出状态代码

c - 如何避免在使用此配置的每个函数中传递配置变量

c - for循环内外指针的值不同

c++ - 以成员函数为参数的结构

go - 使用结构对常量进行分组有哪些副作用

c - 如何使用 sscanf 直接从字符串(从文件读取)输入整数值?

c++ - 无法让 Makefile 工作

c++ - 一元 '*' 的无效类型参数(有 'bool' )