【发布时间】:2012-11-24 14:03:40
【问题描述】:
所以基本上,我要做的是将数组作为参数传递。我想出了这样的想法:
#include <stdio.h>
#include <stdlib.h>
int x;
void function(int array[][x]){
//stuff here
}
int main(){
x = random(10);
int array[10][x];
//initialize array
function(array[10][x]);
}
我认为这应该可行,但它给了我一个提示:
预期为“int (*)[(unsigned int)(x)]”,但参数的类型为“int”
任何帮助将不胜感激。
【问题讨论】:
-
数组声明肯定也是无效的,因为 x 在编译时是未知的。如果我错了,请纠正我,但你应该为此使用 malloc。
int* array = (int*)malloc(10*x) -
@B_o_b:它被称为
VLA(可变长度数组),支持 C99 及更高版本(但不支持 C++) -
@another.anon.coward:我感觉我要学习新东西了。谢谢你。为像我这样的其他人留下评论。
标签: c function multidimensional-array arguments argument-passing