c - 使用返回指针的函数声明为指针是否会导致两个指针链?

标签 c pointers struct

如果我用一个返回指针的函数声明一个指针,我声明的指针与函数返回的指针相同,还是指向函数返回的指针?

示例:

typedef struct foo{
  int n;
} foo;

// this function returns a pointer to a structure foo
foo* returnPointer(){
  foo* tmp = malloc(sizeof(foo));
  tmp->n = 1;
  return tmp;
}

// if I do this, will abc be a pointer to a foo structure (the desired effect)
// or will it be a pointer to another pointer to a foo structure?
foo* abc = returnPointer();

最佳答案

abc 将是您声明的内容。指向 foo 结构的指针。该函数还返回 foo * (指向 foo 结构的指针)。所以存在类型协议(protocol)。

编辑:同样正如@phaazon所说,该函数有错误(需要malloc)。

关于c - 使用返回指针的函数声明为指针是否会导致两个指针链?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40325048/

相关文章:

c - c中的链表子串

C++ 存储模板结构

C-win32 : AttachThreadInput & SetFocus, 64 位:无线索

c - 试图找到两点之间的距离,但我得到了错误的答案 :(

c - 使用 sizeof 运算符

c++ - 更改指向数组的指针以获取特定的数组元素

c++ - 指向函数的智能指针

C 内存地址 - 代码有什么问题?

C 链表显示内容

c - puzzle - 在 C 中以人类可读的形式打印时间; ctime,asctime