linux 第1个参数时在不兼容的指针类型间转换,警告从默认情况下启用的不兼容指针类型传递“”的参数1(warning passing argument 1 of “ ” from incompati...

警告从默认情况下启用的不兼容指针类型传递“”的参数1(warning passing argument 1 of “ ” from incompatible pointer type enabled by default)

randomAssign(int **grid, int size){

int m = rand()%size;

int n = rand()%size;

grid[m][n] = 1;

}

int main()

{

srand(time(NULL));

int i, j, size;

scanf("%d", &size);

int grid[size][size];

for(i=0; i

for(j=0; j

grid[i][j] = 0;

}

}

randomAssign(grid,size); //warning

return 0;

}

我打电话给这个功能时会收到警告。 我尽我所能,但我找不到错误。 哪里出错了? 问候...

randomAssign(int **grid, int size){

int m = rand()%size;

int n = rand()%size;

grid[m][n] = 1;

}

int main()

{

srand(time(NULL));

int i, j, size;

scanf("%d", &size);

int grid[size][size];

for(i=0; i

for(j=0; j

grid[i][j] = 0;

}

}

randomAssign(grid,size); //warning

return 0;

}

I am getting warning when i call the function. I tried all i can do but i couldn't find the mistake. Where is the mistake? Regards...

原文:https://stackoverflow.com/questions/33225607

更新时间:2020-03-02 16:18

最满意答案

数组和指针是不同的。 数组是特定类型的一系列连续元素。 指针是一个小对象,它保存另一个对象的地址。

您的函数需要一个指向另一个指针的指针。 但是你试图为它提供一个数组。 这不可行。

修复代码的一种方法是使函数接受指向数组的指针(而不是指向指针的指针)。 这可以写成:

void randomAssign(int size, int grid[size][size])

这实际上与拥有int (*grid)[size] ,第一个size是多余的( 详见此处 )但是它有一些文件用途。

Arrays and pointers are different. An array is a series of contiguous elements of a particular type. A pointer is a small object that holds the address of another object.

Your function expects a pointer that points to another pointer. However you tried to supply an array to it. This can't possibly work.

One way to fix your code would be to make the function accept a pointer to an array (not a pointer to a pointer). This could be written:

void randomAssign(int size, int grid[size][size])

This is actually the same as having int (*grid)[size], the first size is redundant (see here for detail) however it serves some documentary purpose.

2017-05-23

相关问答

无论generic_ptr类型是什么,显然编译器无法自动将'char *'类型转换为通用ptr类型。 尝试做第二个arg的显式案例来弹出和顶部,例如: pop(p_s,(generic_ptr)p_data) Well whatever the generic_ptr type is, obviously the compiler is not able to automatically cast your 'char *' type into generic ptr type. Try doin

...

您的wordsearh函数定义了2个参数: int word_search(char word[100], char str[100])

你用4个参数调用; word_search(&fileptr, word, &length, &num);

你不想从main调用函数search_and_count吗? Your wordsearh function is defined with 2 parameters: int word_search(char word[100], char str[

...

你的问题是财产声明: @property (nonatomic, retain) id *serverDateTimeDelegate;

如果你命令 - 双击“id”,你会看到它被定义为: typedef struct objc_object {

Class isa;

} *id;

换句话说, id 已经是对象引用。 因此,在serverDateTimeDelegate之前的*是不必要的和错误的。 拥有它意味着指向对象引用的指针,当你真的只想

...

更换 LeerFich(&equipos); //warning here

通过 LeerFich(equipos);

equipos已经是struct Equipo *类型的,不需要接收它的地址。 Replace LeerFich(&equipos); //warning here

by LeerFich(equipos);

equipos is already of type struct Equipo *, there is no need to take its add

...

根本原因: 数组不是指针! 请注意,没有从dict[][]到dict **隐式转换,因此也是错误。 当你将一个数组传递给一个函数时,它将作为指向其第一个元素的指针衰减。在二维数组的情况下,第一个元素是一个单维数组本身,所以你需要一个指向数组的指针而不是双指针。 Soultion: 你需要修改你的函数原型来匹配你传递的类型 。 void add_word(char dict[][WORD_LENGTH], int index, char *word);

void print_dictionary(c

...

如果要将array作为函数参数发送,则必须发送array的基址,并使用pointer to the same type的pointer to the same type接收它。 就像是 您正在接收它作为指针,但您发送的是int而不是数组的基址, calculation(myArray[i], myArrayTwo[i], i);

^^^^^^^^^^ ^^^^^^^^^^^^^

int not int*

对于数组myArray[50]

...

如果在分配函数指针时删除了强制转换,则会得到: tmp.c: In function ‘main’:

tmp.c:13:22: warning: initialization from incompatible pointer type [enabled by default]

callDisplay fn = &disp;

即使通过转换为您调用函数指针时调用了未定义行为的不同类型的函数指针,强制转换也会抑制此警告。 基本上,你永远不需要转换函数指针,因为它会隐藏任何这样的警告。 如果

...

出现警告的原因是C标准的以下引用 6.3.2.3指针 8指向一种类型的函数的指针可以转换为指向另一种类型的函数的指针,然后再返回; 结果应该等于原始指针。 如果转换的指针用于调用类型与引用类型不兼容的函数,则行为未定义。 这两个函数兼容,它们的参数应具有兼容的类型 6.7.6.3函数声明符(包括原型) 15对于兼容的两种功能类型,两者都应指定兼容的返回类型.146)此外,参数类型列表(如果两者都存在)应在参数数量和省略号终止符的使用中一致; 相应的参数应具有兼容的类型。 在您的函数中,参数被声明为

...

数组和指针是不同的。 数组是特定类型的一系列连续元素。 指针是一个小对象,它保存另一个对象的地址。 您的函数需要一个指向另一个指针的指针。 但是你试图为它提供一个数组。 这不可行。 修复代码的一种方法是使函数接受指向数组的指针(而不是指向指针的指针)。 这可以写成: void randomAssign(int size, int grid[size][size])

这实际上与拥有int (*grid)[size] ,第一个size是多余的( 详见此处 )但是它有一些文件用途。 Arrays an

...

你因为多个typedef而感到困惑。 LIST是一种表示struct listhead指针的类型。 因此,您希望ListCreate函数返回LIST ,而不是LIST * : LIST ListCreate(void)

上面说:如果可以, ListCreate()函数将返回指向新列表头部的指针。 然后你需要从return &headpoolp-1;更改函数定义中的return语句return &headpoolp-1; return headpoolp-1; 。 这是因为你想要返回最后一个可用的

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值