c语言结构体声明函数指针,c语言 结构指针-成都创新互联网站建设

关于创新互联

多方位宣传企业产品与服务 突出企业形象

公司简介 公司的服务 荣誉资质 新闻动态 联系我们

c语言结构体声明函数指针,c语言 结构指针

C语言中如何在结构体里写函数,指向函数指针问题。

改动如下:

在含山等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站建设 网站设计制作定制制作,公司网站建设,企业网站建设,成都品牌网站建设,成都营销网站建设,成都外贸网站建设,含山网站建设费用合理。

#include stdio.h

#includestring.h

enum gender

{

male,famale

};

typedef struct course

{

char coursename[10];

int coursescore;

}STC;

typedef struct student

{

long studentnumber;

char name[20];

int age;

enum gender sex;

STC coursescore[3];

struct student *next;

void (*ptooutput)(struct student*); // 改动1:改成指针,与output函数匹配

}STD;

void output(struct student *p)

{

int i;

printf("学生%s:\n",p-name);

printf("他的学号是%ld\n",p-studentnumber); // 改动2:学号

printf("他的年龄是%d\n",p-age); // 改动3:年龄

if(p-sex==0)

{printf("他的性别是男\n");}

else

{printf("他的性别是女\n");}

for(i=0;i3;i++)

{

printf("他的%s",p-coursescore[i].coursename);

printf("成绩是%d\n",p-coursescore[i].coursescore);

}

}

main()

{

STD a;

a.studentnumber=1011110201;

strcpy(a.name,"叶超");

a.age=19;

a.sex=male;

strcpy(a.coursescore[0].coursename,"数学");

a.coursescore[0].coursescore=87;

strcpy(a.coursescore[1].coursename,"英语");

a.coursescore[1].coursescore=72;

strcpy(a.coursescore[2].coursename,"C语言");

a.coursescore[2].coursescore=66;

a.ptooutput = output; // 改动4:设置打印函数指针

a.ptooutput(a); // 改动5:输入参数为指针

}

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++的面向对象的类 

//十分好用。 

#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语言结构体声明函数指针,c语言 结构指针
标题路径:http://kswsj.cn/article/dssijco.html

其他资讯