我有
int list[] = {1, 2, 3};
Run Code Online (Sandbox Code Playgroud)
如何获得大小list?
我知道对于char数组,我们可以strlen(array)用来查找大小,或者'\0'在数组末尾检查.
我尝试sizeof(array) / sizeof(array[0])了一些答案,但它只适用于主?例如:
int size(int arr1[]){
return sizeof(arr1) / sizeof(arr1[0]);
}
int main() {
int list[] = {1, 2, 3};
int size1 = sizeof(list) / sizeof(list[0]); // ok
int size2 = size(list_1); // no
// size1 and size2 are not the same
}
Run Code Online (Sandbox Code Playgroud)
为什么?
对不起这个菜鸟问题我只是有点困惑.
如果我在main中有一个结构数组,我想传递给一个函数:
struct MyStruct{
int a;
int b;
char c;
mayarray[5];
};
MyStruct StructArray[10];
myFunction(StructArray[])
Run Code Online (Sandbox Code Playgroud)
传递给一个函数:
void myFunction(struct MyStruct PassedStruct[])
{
PassedStruct[0].a = 1;
PassedStruct[0].b = 2;
// ... etc
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,会调用这样的函数修改数据StructArray吗?我需要它.这可以通过参考来打电话吗?我有点困惑.我如何更改它,以便当我将结构数组传递给函数时,该函数将修改数组StructArray?我正在使用视觉工作室顺便说一下.
谢谢.