[英]Array pointer to find largest number
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a= malloc(sizeof(int)*10);
scanf("%d %d",a,a+1);
if(*a<*(a+1))
{
*a=*(a+1);
}
printf("%d",*a);
return 0;
}
是否可以使用相同的數組指針輸入2個數字並在其中找到最大的數字,如上面的代碼所示?
是的,它將起作用,盡管使用括號表示法對數組元素可能更易讀。 您還只需要為2個元素分配空間。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *a = malloc(sizeof(int) * 2);
scanf("%d %d", &a[0], &a[1]);
if(a[0] < a[1])
{
a[0] = a[1];
}
printf("%d", a[0]);
return 0;
}
是的你可以。 因為當您說*a
您指向數組的第0個位置並在那里獲取值;當您說*(a+1)
您指向數組的第1個位置。 與&a
和&(a+1)
相似。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.