
C将数组参数视为指针,因为这样做更省时且更高效。尽管我们可以将数组的每个元素的地址作为参数传递给函数,但这样做会更耗时。所以最好将第一个元素的基地址传递给函数,例如:
void fun(int a[]) {
…
}
void fun(int *a) { //more efficient.
…..
}Here is a sample code in C:
#include
void display1(int a[]) //printing the array content
{
int i;
printf("</p><p>Current content of the array is: </p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/%E9%98%BF%E9%87%8C%E4%BA%91%E8%99%9A%E6%8B%9F%E6%95%B0%E5%AD%97%E4%BA%BA">
<img src="https://img.php.cn/upload/ai_manual/001/503/042/68b6c5d39a38c971.png" alt="阿里云-虚拟数字人">
</a>
<div class="aritcle_card_info">
<a href="/ai/%E9%98%BF%E9%87%8C%E4%BA%91%E8%99%9A%E6%8B%9F%E6%95%B0%E5%AD%97%E4%BA%BA">阿里云-虚拟数字人</a>
<p>阿里云-虚拟数字人是什么? ...</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="阿里云-虚拟数字人">
<span>2</span>
</div>
</div>
<a href="/ai/%E9%98%BF%E9%87%8C%E4%BA%91%E8%99%9A%E6%8B%9F%E6%95%B0%E5%AD%97%E4%BA%BA" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="阿里云-虚拟数字人">
</a>
</div>
<p>");
for(i = 0; i < 5; i++)
printf(" %d",a[i]);
}
void display2(int *a) //printing the array content
{
int i;
printf("</p><p>Current content of the array is: </p><p>");
for(i = 0; i < 5; i++)
printf(" %d",*(a+i));
}
int main()
{
int a[5] = {4, 2, 7, 9, 6}; //initialization of array elements
display1(a);
display2(a);
return 0;
}输出
Current content of the array is: 4 2 7 9 6 Current content of the array is: 4 2 7 9 6
以上就是为什么C将数组参数视为指针?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号