C语言 错误:赋值使指针从整数而不进行强制转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14243969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:58:40  来源:igfitidea点击:

Error:Assignment makes pointer from integer without a cast

c

提问by Elia

int findNumber(char *exp,int i,int *num)    
{    
    int k=i;    
    char *p;    
    p=exp[i]; //<-- here
    while(*p>='0'&&*p<='9')    
    {
        (*num)=(*num)*10+(*p);
        k++;
        p++;
    }
    return k;
}

i keep getting that error in line: (p=exp[i];) Im trying to send a char array, and (i,num) integers, the 'i' im just putting it to be 0 for now, until the code works so dont give attention to it. but the function should return the place of the first character in "exp" that is not a number, with being sure that all the ones before are numbers.

我不断收到该错误:(p=exp[i];) 我正在尝试发送一个字符数组和 (i,num) 整数,'i' 我只是暂时将它设置为 0,直到代码作品所以不要关注它。但是该函数应该返回“exp”中第一个不是数字的字符的位置,并确保之前的所有字符都是数字。

回答by simonc

pis a char*so you need to assign a pointer to it but exp[i]returns a single charelement from an array. Try

p是 achar*所以你需要为它分配一个指针,但从数组中exp[i]返回一个char元素。尝试

p = &exp[i];

or

或者

p = (exp + i);

instead.

反而。

回答by hmatar

do correct the line to correspond to the following p=&(exp[i]);this means that you are assigning the pointer of exp[i] to p.

更正该行以对应于以下内容,p=&(exp[i]);这意味着您将 exp[i] 的指针分配给 p。

回答by Hyman

You need take the address-of instead of and then pass to p, that's a pointer. In other words,you are assign charto a char* returned from value atiindex inexp` array.

您需要使用 address-of 而不是,然后传递给p,这是一个指针。换句话说,你被分配 char给一个char* returned from value ati index inexp` 数组。

Try this: p = &exp[i];

尝试这个: p = &exp[i];