我正在使用动态数组,这是声明:
int *vetor = (int *) malloc (tam*sizeof(int));
vetorAleatorio (vetor, tam); //chamando função abaixo
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将它作为参数传递给此函数时:
void vetorAleatorio(int **vet, int size) {
int i;
for (i=0; i<size; i++)
vet[i] = rand() %1000;}
Run Code Online (Sandbox Code Playgroud)
我有以下错误:
[Warning] assignment makes pointer from integer without a cast
[Warning] passing arg 1 of `vetorAleatorio' from incompatible pointer type
Run Code Online (Sandbox Code Playgroud)
有人知道这是怎么回事吗?
你的函数语法:
void vetorAleatorio(int **vet, int size)
Run Code Online (Sandbox Code Playgroud)
应该:
void vetorAleatorio(int *vet, int size)
^
// remove one *
Run Code Online (Sandbox Code Playgroud)
[Warning]赋值使得整数指针没有强制转换
如果使用双*作为 int **对vet,那么它的类型不匹配,如下所示:
vet[i] = rand() %1000
^ ^
| int // rand() %1000 returns a int
type is int*
// vet[i] == *(vet + i) == *(pointer to pointer of int) = pointer int = int*
Run Code Online (Sandbox Code Playgroud)
警告-2:从不兼容的指针类型传递`vetorAleatorio'的arg 1
理解在您的代码中,您根据void vetorAleatorio(int **vet, int size)声明以错误的方式调用函数:vetorAleatorio (vetor, tam);,您将int =指针传递给int,而参数需要指向int的指针地址=指向int的指针.
你只需要按照我的建议进行一次整改.