c - 将指向函数的指针作为参数传递的不同方法
问题描述
我不明白为什么当我调用这样的函数时:
typedef struct str_node{
int data;
struct str_node *next;
}node;
void begin(node *num);
int main(){
node a;
begin(&a);
我必须作为参数通过&a
,我不能简单地通过a
。确实,当我遇到这种情况时:
void begin(node *head);
int main(){
node *a;
begin(a);
我必须通过a
而不是&a
。但是当我遇到这种情况时:
void begin(node **head);
int main(){
node *a;
begin(&a);
我必须再次通过&a
而不是a
。此外,为什么我总是必须使用node *a
而不是node a
在处理链表的头部时?
解决方案
使用以下行和相应的注释将其可视化:
int a = 5; // say, the compiler allocated 0xABCD0000 memory address for "a"
int *b = &a; // say, the compiler allocated 0xDCBA0000 memory address for "b"
// memory layout so far;
// variable memory address value
// a 0xABCD0000 5
// b 0xDCBA0000 0xABCD0000
foo(a); // pass the value of "a", which is "5".
// possible foo function decoration:
// foo(int x) {}
bar(&a); // pass the memory address of "a", which is "0xABCD0000"
// possible bar function decoration:
// bar(int *x) {}
baz(b); // pass the value of "b", which is, in turn, the memory address of "a"
// possible baz function decoration:
// baz(int *x) {}
boo(&b); // pass the memory address of "b", which is "0xDCBA0000"
// possible boo function decoration:
// boo(int **x) {}
// you'll need the last one, if you want to change the address the pointer is pointing at in the sub-function "boo"
推荐阅读
- materialize - 如何在物化中使用 vanilla java-script 初始化轮播全角和指标?
- django - Django Admin中删除操作的奇怪错误
- angular7 - 会话过期时重定向到登录页面
- javascript - 使用 javascript 删除 django formset
- typescript - 编译打字稿时找不到名称“符号”
- android - 搜索栏进度动画?
- ios - 调用 invalidateLayout() 或 reloadData() 以在设备旋转后重新加载从情节提要创建的 UICollectionView
- haskell - 通过过滤每两个数字的序列,使用推导在 Haskell 中生成一个列表
- c# - C# 删除 flowLayoutPanel 中动态创建的控件
- c# - 删除后如何从选定行datagridview中删除记录并更新数据库?