黑马程序员---------C语言 关于数组被当作函数参数传递的问题

本文详细解析了将数组作为函数参数传递时的指针使用方式,并通过示例展示了如何正确地在C语言中找到数组中的最大元素。同时,解释了数组作为参数传递时被当作指针的原因及影响。

                 ------<a href="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

当一个数组当作函数参数传递时,会被当作指针使用。下面举一例子

编写一个函数 :int arrayMax(int a[],int count),找出数组中元素最大值

 #include
int arryMax (int a[] , int count);
int main ()
{  int ages [] = {10 , 23 , 43 , 45 , 56 ,76 };
 int temp = arryMax ( ages , sizeof(ages)/sizeof(int));
 printf ("最大值为%d\n",temp);
 return 0 ;
}
int arryMax (int a[] , int count )
{
 int temp = a[0];
 for (int i = 0 ; i < count ; i++)
 {
  if (a[i+1] > a[i])
  {
   temp = a[i+1] ;
  }
 }
 
 return temp ;
}

这个题以上做法是正确的 输出结果是76,

下面说一下如果直接用用数组当作函数参数传递,


#include
int arryMax (int a[] , int count);
int main ()
{  int ages [] = {10 , 23 , 43 , 45 , 56 ,76  };
   int size1 = sizeof(ages);
     printf("ades=%d\n",size1);
 
 return 0;
}
int arryMax (int a[]  )
{
 int size = sizeof(a);
 printf("a=%d\n",size);
 
 return 0;
}

}这次有两个输出 一个是ades = 24 a = 8,所以当数组作为函数参数时,会被当作指针来传递,以后一定会注意

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值