一个关于C语言程序的问题,拜托~~

来源:百度知道 编辑:UC知道 时间:2024/06/01 07:24:50
#include<stdio.h>
#include<string.h>
#define N 5
struct student
{ char name[10];int score;};

void sort(struct student stud[],int n)
{int i,j;
struct student p;
for(i=1;i<n;i++)
{p=stud[i];
for(j=i-1;(j>=0)&&(p.score>stud[j].score);j--)
stud[j+1]=stud[j];
stud[j+1]=p;
}
}
void main()
{struct student stud[N]={"aaa",60,"bbb",90,"ccc",85,"ddd",65,"yyy",77};int i;
sort(stud,N);
printf("sorted data:\n");
for(i=0;i<N;i++) printf("%s\t%d\n",stud[i].name,stud[i].score);
}

以上这个程序是否能编译通过?个人认为第十九行sort(stud,N);和已声明的sort函数参数类型不符,但是编译时没报错,可以正确运行,请问是怎么回事呢?谢啦~

void sort(struct student stud[],int n)
定义的形参是一个结构数组和一个整数,
sort(stud,N)传递的是结构数组的首址和一个整数,没问题。因为在C里面是没办法把数组作为参数传递的

类型不符么?
sort(stud,N);相当于sort(stud,5);5就是int型啊。

能编译通过啊,参数类型是符合的,struct student stud[]是student类型的数组,而stud也是student类型的数组啊,int n是整形的数n 而N也是整型的啊,因为你#define N 5,又把N用在stud[N]所以,N是整形的

能编译 能运行。。

sort(stud,N);

数组类型引用时一般 别认为时指针类型。。

stud 可以认为是 student stud[N] 的头指针。。

能通过啊,而且类型是正确的

当然能运行了 stud传递结构数组stud[]的首地址