47

I've just started learning C and I've been running some simple programs using MinGW for Windows to understand how pointers work. I tried the following:

#include <stdio.h>

int main(){
    int *ptr;
    *ptr = 20;
    printf("%d", *ptr);
    return 0;
}

which compiled properly but when I run the executable it doesn't work - the value isn't printed to the command line, instead I get an error message that says the .exe file has stopped working.

However when I tried storing the value in an int variable and assign *ptr to the memory address of that variable as shown below:

#include <stdio.h>

int main(){
    int *ptr;
    int q = 50;
    ptr = &q;
    printf("%d", *ptr);
    return 0;
}

it works fine.

My question is, why am I unable to directly set a literal value to the pointer? I've looked at tutorials online for pointers and most of them do it the same way as the second example.

Any help is appreciated.

4

4 回答 4

83

问题是您没有初始化指针。你已经创建了一个指向“任何你想要的地方”的指针——它可能是一些其他变量的地址,或者你的代码中间,或者一些根本没有映射的内存。

您需要int在内存中的某处创建一个变量int *以指向该变量。

您的第二个示例执行此操作,但它执行与此处无关的其他操作。这是您需要做的最简单的事情:

int main(){
    int variable;
    int *ptr = &variable;
    *ptr = 20;
    printf("%d", *ptr);
    return 0;
}

在这里,int变量没有被初始化——但这很好,因为你只是要用20. 关键是指针被初始化为指向variable. 事实上,如果你愿意,你可以分配一些原始内存来指向:

int main(){
    void *memory = malloc(sizeof(int));
    int *ptr = (int *)memory;
    *ptr = 20;
    printf("%d", *ptr);
    free(memory);
    return 0;
}
于 2013-07-15T23:50:12.537 回答
27

第一个带有评论的程序

#include <stdio.h>

int main(){
    int *ptr;             //Create a pointer that points to random memory address

    *ptr = 20;            //Dereference that pointer, 
                          // and assign a value to random memory address.
                          //Depending on external (not inside your program) state
                          // this will either crash or SILENTLY CORRUPT another 
                          // data structure in your program.  

    printf("%d", *ptr);   //Print contents of same random memory address
                          // May or may not crash, depending on who owns this address

    return 0;             
}

带评论的第二个程序

#include <stdio.h>

int main(){
    int *ptr;              //Create pointer to random memory address

    int q = 50;            //Create local variable with contents int 50

    ptr = &q;              //Update address targeted by above created pointer to point
                           // to local variable your program properly created

    printf("%d", *ptr);    //Happily print the contents of said local variable (q)
    return 0;
}

关键是你不能使用指针,除非你知道它被分配给你自己管理的地址,要么将它指向你创建的另一个变量,要么指向 malloc 调用的结果。

之前使用它会创建依赖于未初始化内存的代码,这在最好的情况下会崩溃,但在最坏的情况下有时会工作,因为随机内存地址恰好位于程序已经拥有的内存空间内。如果它覆盖了您在程序中其他地方使用的数据结构,上帝会帮助您。

于 2013-07-16T00:30:17.343 回答
3

在第一个示例中,ptr 尚未初始化,因此它指向一个未指定的内存位置。当您将某些内容分配给这个未指定的位置时,您的程序就会崩溃。

在第二个示例中,当您说 ptr = &q 时设置了地址,所以您没问题。

于 2013-07-15T23:55:21.687 回答
-1

您可以为指针设置一个值,但是一旦您使用“new”为它请求内存。这就是您的代码的外观

int main(){
    int *ptr;
    ptr = new int;    //ask for memory
    *ptr = 20;
    printf("%d", *ptr);
    return 0;
}
于 2021-06-17T01:01:02.590 回答