RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
c语言函数指针结构体指针,c语言怎么调用结构体指针函数

C语言结构体内部的函数指针有什么意义

//在结构体中包含函数指针,

创新互联建站专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,成都移动机房服务有保障!

//这样,可以使用结构体,调用函数。

//这个有点像C++的面向对象的类 

//十分好用。 

#include "stdio.h"  

struct DEMO  

{  

int x,y;  

int (*func)(int,int); //函数指针  

};  

int add2(int x,int y)  

{  

return x+y;  

}  

int main()  

{

int ret=0;

struct DEMO demo;  

demo.func=add2; //结构体函数指针赋值  

ret=demo.func(3,4);

printf("func(3,4)=%d\n",ret);  

}

C语言函数怎么传结构体指针

#include stdio.h

#include stdlib.h

#define LINE_MAX 80

struct body{

char data[100]; //要定义成数组才可以,不然,还要去分配内存

int num;

};

void create(struct body *bd); //结构体定义后,才能使用结构体类型,所以,移动到定义之后

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

int choose;

struct body *bd;

bd = (struct body*)malloc(sizeof(struct body));

while(1)

{

printf("        *******************欢迎来到文章编辑系统********************\n");

printf("1.   创建新文本\n");

printf("2.   统计文本\n");

printf("5.   退出系统\n");

printf("请选择你需要的功能的序号:");

scanf("%d",choose);

switch(choose)

{

case 1:

printf("创建新文本\n");

create(bd);

continue;

case 2:

printf("统计文本\n");

continue;

case 5:

printf("谢谢您的使用!\n");

break;

default:

printf("请正确输入!\n");

continue;

}

if(choose == 5)

break;

}

return 0;

}

void create(struct body *bd)

{

printf("编辑文本,Enter键保存\n");

scanf("%s",bd-data);//结构体指针引用成员用-,  同时,格式串应该是%s

printf("您输入的文本是:%s\n",bd-data); //同上

}

c语言函数指针作为结构体的问题

首先解释一下

st

的问题吧:st

就是取结构体的

st

的地址传给结构体内的函数

p

o,

根据前面

st

的定义,也就是传给

print

power。这样

print

power

函数就可以读取结构体中的

i

x

值。

然后沿着各个思路,可以写出

print

power

函数,如下:

void

print(ST

*st){

printf

("%g",

st-x);

}

void

power(ST

*st){

int

k;

double

y=1;

for

(k=0;k

i;k++)

y*=st-x;

st-x

=

y;

}

不过这里有个问题,就是你之前的

struct

中定义的函数指针是没有参数的,但是主函数调用时是有参数的,这是矛盾的呀。要改一下:

struct

ST{

int

i;

double

x;

void

(*o)(ST*);

void

(*p)(ST*);

}

;

就没有问题了。


当前标题:c语言函数指针结构体指针,c语言怎么调用结构体指针函数
文章URL:http://scyingshan.cn/article/phshod.html