C语言 初始化使指针从整数而不进行强制转换 - C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33404478/
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 12:11:28  来源:igfitidea点击:

Initialization makes pointer from integer without a cast - C

cpointers

提问by Jeff H

Sorry if this post comes off as ignorant, but I'm still very new to C, so I don't have a great understanding of it. Right now I'm trying to figure out pointers.

对不起,如果这篇文章被认为是无知的,但我对 C 仍然很陌生,所以我对它没有很好的理解。现在我正试图找出指针。

I made this bit of code to test if I can change the value of b in the change function, and have that carry over back into the main function(without returning) by passing in the pointer.

我编写了这段代码来测试我是否可以在更改函数中更改 b 的值,并通过传入指针将其带回到主函数中(不返回)。

However, I get an error that says.

但是,我收到一条错误消息。

Initialization makes pointer from integer without a cast
    int *b = 6

From what I understand,

据我了解,

#include <stdio.h>

int change(int * b){
     * b = 4;
     return 0;
}

int main(){
       int * b = 6;
       change(b);
       printf("%d", b);
       return 0;
}

Ill I'm really worried about is fixing this error, but if my understanding of pointers is completely wrong, I wouldn't be opposed to criticism.

我真的很担心修复这个错误,但如果我对指针的理解完全错误,我不会反对批评。

回答by Soren Goyal

To make it work rewrite the code as follows -

要使其工作,请按如下方式重写代码 -

#include <stdio.h>

int change(int * b){
    * b = 4;
    return 0;
}

int main(){
    int b = 6; //variable type of b is 'int' not 'int *'
    change(&b);//Instead of b the address of b is passed
    printf("%d", b);
    return 0;
}

The code above will work.

上面的代码将起作用。

In C, when you wish to change the value of a variable in a function, you "pass the Variable into the function by Reference". You can read more about this here - Pass by Reference

在 C 中,当您希望更改函数中变量的值时,您可以“通过引用将变量传递给函数”。您可以在此处阅读更多相关信息 -通过引用传递

Now the error means that you are trying to store an integer into a variable that is a pointer, without typecasting. You can make this error go away by changing that line as follows (But the program won't work because the logic will still be wrong )

现在错误意味着您正在尝试将一个整数存储到一个指针变量中,而不进行类型转换。您可以通过如下更改该行来消除此错误(但程序将无法运行,因为逻辑仍然是错误的)

int * b = (int *)6; //This is typecasting int into type (int *)

回答by BobRun

Maybe you wanted to do this:

也许你想这样做:

#include <stdio.h>

int change( int *b )
{
  *b = 4;
  return 0;
}

int main( void )
{
  int *b;
  int myint = 6;

  b = &myint;
  change( &b );
  printf( "%d", b );
  return 0;
}

回答by milevyo

#include <stdio.h>

int change(int * b){
     * b = 4;
     return 0;
}

int main(){
       int  b = 6; // <- just int not a pointer to int
       change(&b); // address of the int
       printf("%d", b);
       return 0;
}

回答by udarH3

Maybe too late, but as a complement to the rest of the answers, just my 2 cents:

也许为时已晚,但作为对其余答案的补充,只需我的 2 美分:

void change(int *b, int c)
{
     *b = c;
}

int main()
{
    int a = 25;
    change(&a, 20); --> with an added parameter
    printf("%d", a);
    return 0;
}

In pointer declarations, you should only assign the address of other variables e.g "&a"..

在指针声明中,您应该只分配其他变量的地址,例如“&a”..