gpt4 book ai didi

Debugging error ' pointer from integer without a cast '(调试错误‘来自整数的指针没有强制转换’)

转载 作者:bug小助手 更新时间:2023-10-28 11:40:35 28 4
gpt4 key购买 nike



While running the code:

在运行代码时:


#include<stdio.h>
void insert(int len,int ele,int pos, int ar[]){
int x= pos,i;
for (i=0;i<len;i++){
ar[pos+1]=ar[pos];
pos++;
}
ar[x]=ele;
for (i=0;i<len+1;i++){
printf("%d",ar[i]);
}


}
int main(){
int len,pos,ele,i;
int ar[len];
printf("Enter length of array");
scanf("%d",&len);
for( i=0;i<len;i++){
printf("Enter element ");
scanf("%d",&ar[i]);
}
printf("Enter element to insert ");
scanf("%d",&ele);
printf("Enter position ");
scanf("%d",&pos);
insert(len,ele,pos,ar[len]);
}

I am getting this error:

我收到以下错误:


test.c: In function 'main':
test.c:26:5: warning: passing argument 3 of 'insert' makes pointer from integer without a cast [enabled by default]
insert(ele,pos,ar[5]);
^
test.c:2:6: note: expected 'int *' but argument is of type 'int'
void insert(int ele,int pos, int ar[5]){

I am a beginner in programming who's learning C for the Uni course. I am trying to make sense of the error but I am unable to do. Any help would be extremely appreciated.

我是一个初学编程的人,正在为大学课程学习C。我试图理解这个错误,但我做不到。任何帮助都将不胜感激。


更多回答

Whatever you do, do not use a cast

无论你做什么,都不要使用石膏

insert() needs a pointer as its 4th argument... you are passing an invalid element of the array. insert(len,ele,pos,ar[len]); /* ar[len] is invalid, it's an int (if it was valid), not a pointer */

Insert()需要一个指针作为其第四个参数...您传递的数组元素无效。INSERT(len,ele,pos,ar[len]);/*ar[len]无效,它是int(如果有效),而不是指针*/

@pmg So what should I change in this code?

@PMG,那么我应该在这段代码中更改什么?

You should read the value of len before using it uninitialized as the size of an array. You should simply pass ar to the function. You'll still have a problem since insert seems to think there is extra space at the end of the array and there is not.

在未将其初始化为数组大小之前,应读取len的值。您只需将ar传递给函数即可。您仍然会有一个问题,因为INSERT似乎认为在数组的末尾有额外的空间,但实际上没有。

As RetiredNinja says, to solve your immediate problem and pass a pointer (to the 1st element of the array) use just the array identifier ar on its own. This is automatically converted to a pointer to its first element. There may be other problems with the code, I didn't check.

正如RetiredNimpa所说,要解决眼前的问题并传递一个指针(指向数组的第一个元素),只需使用数组标识符ar。它会自动转换为指向其第一个元素的指针。代码可能还有其他问题,我没有检查。

优秀答案推荐


    int ar[len];


Is a way to declare an array of int. The name it designates for the array is ar, so when you want to refer to that array itself, just ar is what you use.

是声明int数组的一种方式。它为数组指定的名称是ar,所以当您想引用该数组本身时,只需使用ar即可。


If, in an expression, you then say ar[len], that designates an int at index len in array ar -- which in this case happens to be out of bounds for the array, so the code is doubly wrong.

如果在表达式中指定ar[len],这表示数组ar中的索引len处有一个int--在本例中恰好超出了数组的界限,因此代码大错特错。


Thus, here:

因此,在这里:



    insert(len,ele,pos,ar[len]);


you mean

你是说


    insert(len,ele,pos,ar);

, or, equivalently,

,或者,等同地,


    insert(len,ele,pos,&ar[0]);


The insert function expects the ar argument to be a pointer to an int. By passing an element of an int array, you have passed just an int.

INSERT函数期望ar参数是指向int的指针。通过传递整型数组的元素,您只传递了一个整型。


This is a warning rather than an error because a pointer is a numeric type, so you can do this, but you shouldn't. You've just passed something that isn't a valid pointer. Your function then tries to derefence that (not really a) pointer with:

这是一个警告,而不是错误,因为指针是数值类型,所以您可以这样做,但您不应该这样做。您刚刚传递了一些不是有效指针的内容。然后,您的函数尝试使用以下命令来减少该指针(不是真正的指针):


ar[pos + 1] = ar[pos];

Remember that ar[pos] is equivalent to *(ar + pos).

记住,ar[pos]等同于*(ar+pos)。


This invokes the dreaded undefined behavior.

这会引发可怕的未定义行为。


You should just be passing ar to insert. The array decays to a pointer to its first element.

您只需传递ar以插入即可。该数组衰减为指向其第一个元素的指针。


insert(len, ele, pos, ar);

Note also that C (and many other languages) has arrays with indexes starting at 0, so for an array arr with length len, the last valid element in the array is at arr[len-1].

还要注意,C语言(和许多其他语言)都有索引从0开始的数组,因此对于长度为len的数组arr,数组中的最后一个有效元素在arr[len-1]。


更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com