全站数据
8 4 2 0 5 8 1

单链表如何定义一个指针

该学习了哦 | 简单学习,快乐成才!         
问题更新日期:2024-10-11 06:30:27

问题描述

单链表如何定义一个指针,在线求解答
精选答案
最佳答案

单链表通过调用函数指针的步骤:

第一步,定义函数指针

int* (*pfun)(int*,int*);

这里调用了一个数据类型为int *的函数指针,其中两个参数为两个int*。

第二步,定义指针函数

int* fun(int*, int*);

这里函数的返回值是int *。

第三步,实现函数指针

int* fun(int* a, int* b){

int* ret = 0;

(*ret) = (*a) + (*b);

return ret;

}

第四步,把函数的地址赋值给函数指针

int main(int argc, char** argv){

int a = 3, b = 2,c = 0;

pfun = fun;

c = *((*pfun)(&a,&b));

rt_kprintf("c is %d

", c);

return0;

}

其中最关键的是赋值和调用,赋值时采用的是pfun = fun;,而间接调用函数时采用的是*((*pfun)(&a,&b));。

其他回答

struct node{ int date; //举例 node *pnext //这个指向下一个节点对吧。 }; node a, b; a.pnext=&b; 那么你就可以用 a.pnext.data来访问节点b的数据了。 关键是理解节点里面的next指针指向的是一个node的结构体。