#include<stdio.h>//用于求m個班,每班n個人中分數最大值
#include<stdlib.h>
int MAX(int *h,int *l,int *maxscore,int m,int n);//用于得出最大值的值及位置
int main(void)
{
int *score; // 各學生的分數
int i,j,m,n;
int h,l;//用于記錄最大值所在行列
int biggest;
printf("please enter the num of the classes and the students\n");
scanf("%d%d",&m,&n);
score=(int *)calloc(m*n,sizeof(int));
if(score==NULL)
{
printf("No enough memory\n");
exit(0);
}
printf("please enter the score\n");;
for(j=0;j<m;j++)//輸入各學生分數
{
for(i=0;i<n;i++)
{
scanf("%d",&score[j*n+i]);
}
}
biggest=MAX(&h,&l,score,m,n);//得到最大值的值及位置
printf("maxscore=%d,class=%d,number=%d\n",biggest,h+1,l+1);
free(score);
return 0;
}
int MAX(int *h,int *l,int *maxscore,int m,int n)
{
int i,j,max;
max=maxscore[0];
*h=0;
*l=0;
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
if(maxscore[j*m+i]>max)
{
max=maxscore[j*m+i];
*h=j;
*l=i;
}
}
}
return max;
}
為什么第26行呼叫函式括號中的score不用加取址符
uj5u.com熱心網友回復:
score=(int *)calloc(m*n,sizeof(int)); //score的值就是這里申請得到的記憶體地址,&score是score變數自己的地址,這兩個地址是不同的,而且&score是個二級指標**,和MAX的引數型別也不匹配MAX處理,是為了遍歷上面申請得到的記憶體地址,而不是為了遍歷score變數自己的地址&score(因為score變數自己的地址沒什么東西,只有上面申請的記憶體地址才有需要的資訊),所以傳score就可以了
以下能理解嗎?
int a; //a是int型別,&a是什么型別?是int*型別
同理
int *a; //a是int*型別,&a是什么型別?是int**型別
所以
int a[]={1,2,3};
int *p = a;
//p的值就是a的地址,&p是p自己的地址(是個二級指標),遍歷&p是不會得到1,2,3的,只有遍歷a(也就是p的值)才會得到1,2,3
所以MAX要score而不是&score
uj5u.com熱心網友回復:
因為score是指標,一重指標,正好和形參型別匹配。可以看一下score是malloc申請的空間轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/235826.html
標籤:C語言
上一篇:指標變數的*什么意思
下一篇:請問哪里出錯了
