在c语言中 函数和结构体的区别

Python089

在c语言中 函数和结构体的区别,第1张

C语言中结构体和函数有着本质上的不同。

结构体是用户自己定义的一中数据类型,比如说你想要把学生信息作为一个整体存放在一个数据类型中,可以定义结构体,其中某个成员存放学号,某个存放姓名等等。

但是函数就完全不同了,函数是为了实现某种功能,比如你要在一堆学生中寻找某一个学生可以用函数来实现。

首先,

在函数

double

ave(double

g.gr1,double

g.gr2,double

g.gr3)

{

ave=(g.gr1+g.gr2+g.gr3)/3.0

return

ave

}

中,用到局部变量ave,但是并未定义。(此变量ave与主函数的变量ave不同,不能混淆)。

为了避免混淆,函数名与变量名最好不要用同样的名字,以免自己都不清楚。

另外,结构体中

char

name;难道名字只有一个字符吗?好歹也该设一个字符数组吧。否则,只要你在这一部分输入字符超过一个就会造成越界,影响整个程序的运行。

scanf("%d%s",&g.num,g.name)这一句,g.name前遗漏了&符号。

在主函数里声明函数

double

ave(g.gr1,g.gr2,g.gr3)时,各个变量要说明类型。并且,不应该在主函数内部声明,该声明应该放在外部。最好的方式时,把主函数中要调用的函数放在主函数之前,这样也省去了声明的麻烦。

#include<stdib.h>一句有误,其实可以不需要,或者改为#include<stdlib.h>。

还有一个问题,在英文里,标点符号后面需要有一个空格。

从这里的程序来看,你对结构体基本上都掌握了,但是最基础的部分却不太扎实。不如形参与实参,变量的作用域、定义与声明等。都是前几章的内容。

另外,需要在运行时输入的程序,一定要有足够的提示信息。

修改后的程序如下,经过试验,可以正常运行。

#include<stdio.h>

//#include<stdlib.h>

typedef

struct

student

{

int

num

char

name[20]

double

gr1,

gr2,

gr3

}

student

double

average(double

a,

double

b,

double

c)

{

double

t

t=(a+b+c)/3.0

return

t

}

int

main(void)

{

//struct

student

g

double

ave

//double

ave(g.gr1,g.gr2,g.gr3)

/*定义的函数表示在这里出错了,书不在靠边,这里确实不能确实啥情况。。。*/

printf("\n

Please

input

the

Student

number

and

the

name:

\n")

scanf("%d

%s",

&(g.num),

&g.name)

printf("\n

Please

input

the

score

of

3

courses:

\n")

scanf("%lf

%lf

%lf",

&(g.gr1),

&(g.gr2),

&(g.gr3))

printf("\n

The

information

input

is

as

below:")

printf("\n

Student

number:

%d

",

g.num)

printf("\n

Student

name:

%s

",

&g.name)

printf("\n

The

score

of

3

courses:

%lf,

%lf,

%lf.

",

g.gr1,

g.gr2,

g.gr3)

ave=average(g.gr1,

g.gr2,

g.gr3)

printf("\n

The

average

is

%lf.\n",

ave)

//system("pause")

return

0

}

运行结果如下:

Please

input

the

Student

number

and

the

name:

123456

Michel

Please

input

the

score

of

3

courses:

21.5

210.6

39.6

The

information

input

is

as

below:

Student

number:

123456

Student

name:

Michel

The

score

of

3

courses:

21.500000,

210.600000,

39.600000.

The

average

is

90.566667.

#include <stdio.h>

#include <string.h>

#define N (5)

struct STUDENT

{

    char name[20]

    int age

    float chinese,math

}

void scan(struct STUDENT *a,int n)

void print(struct STUDENT *a,int n)

void sort(void *a,int n,int size,int (*cmp)(void *,void *))

int cmp_score(void *a,void *b)

int cmp_chinese(void *a,void *b)

int cmp_name(void *a,void *b)

int main()

{

    struct STUDENT a[N]

    int i

    scan(a,N)

    sort(a,N,sizeof(*a),cmp_score)

    print(a,N)

    sort(a,N,sizeof(*a),cmp_chinese)

    print(a,N)

    sort(a,N,sizeof(*a),cmp_name)

    print(a,N)

    return 0

}

void scan(struct STUDENT *a,int n)

{

    int i

    for(i=0i<n++i)

    {

        scanf("\n%s %d %f %f",a[i].name,&a[i].age,&a[i].chinese,&a[i].math)

    }

}

void print(struct STUDENT *a,int n)

{

    int i

    for(i=0i<n++i)

    {

        printf("%s %d %f %f\n",a[i].name,a[i].age,a[i].chinese,a[i].math)

    }

}

void sort(void *a,int n,int size,int (*cmp)(void *,void *))

{

    int i,j

    void *t=malloc(size)

    for(i=0i<n-1++i)

    {

        for(j=0j<n-i-1++j)

        {

            if(cmp(a+size*j,a+size*(j+1)))

            {

                memcpy(t,a+size*j,size)

                memcpy(a+size*j,a+size*(j+1),size)

                memcpy(a+size*(j+1),t,size)

            }

        }

    }

    free(t)

}

int cmp_score(void *a,void *b)

{

    return ((struct STUDENT *)a)->chinese+((struct STUDENT *)a)->math<((struct STUDENT *)b)->chinese+((struct STUDENT *)b)->math

}

int cmp_chinese(void *a,void *b)

{

    return ((struct STUDENT *)a)->chinese<((struct STUDENT *)b)->chinese

}

int cmp_name(void *a,void *b)

{

    return strcmp(((struct STUDENT *)a)->name,((struct STUDENT *)b)->name)>0

}