我正在用 C 编程,我想将一个数组传递给一个函数;此函数不能修改数组的元素。函数参数的正确语法是什么?
void func(const Foo array_in[])
或者
void func(Foo const array_in[])
或者他们是一样的?
谢谢你。
最佳答案
您应该使用 指向常量的指针 为了处理:
#include<stdio.h>
void fun(const int *ptr)//pointer to constant - array boils down to pointer when passed to function
{
ptr[0]=9; //ERROR: Read-Only
}
int main(void)
{
int arr[]={1,2,3,4};
fun(arr);
return 0;
}
注意:不要将其与 混淆常量指针
关于c - 如何将数组传递给无法修改它的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60259258/