Directly assigning values to C Pointers

后端 未结 3 642
后悔当初
后悔当初 2020-12-04 10:53

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:

#in         


        
相关标签:
3条回答
  • 2020-12-04 11:21

    First Program with comments

    #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;             
    }
    

    Second Program with comments

    #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;
    }
    

    The key is you cannot use a pointer until you know it is assigned to an address that you yourself have managed, either by pointing it at another variable you created or to the result of a malloc call.

    Using it before is creating code that depends on uninitialized memory which will at best crash but at worst work sometimes, because the random memory address happens to be inside the memory space your program already owns. God help you if it overwrites a data structure you are using elsewhere in your program.

    0 讨论(0)
  • 2020-12-04 11:25

    The problem is that you're not initializing the pointer. You've created a pointer to "anywhere you want"—which could be the address of some other variable, or the middle of your code, or some memory that isn't mapped at all.

    You need to create an int variable somewhere in memory for the int * variable to point at.

    Your second example does this, but it does other things that aren't relevant here. Here's the simplest thing you need to do:

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

    Here, the int variable isn't initialized—but that's fine, because you're just going to replace whatever value was there with 20. The key is that the pointer is initialized to point to the variable. In fact, you could just allocate some raw memory to point to, if you want:

    int main(){
        void *memory = malloc(sizeof(int));
        int *ptr = (int *)memory;
        *ptr = 20;
        printf("%d", *ptr);
        free(memory);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-04 11:38

    In the first example, ptr has not been initialized, so it points to an unspecified memory location. When you assign something to this unspecified location, your program blows up.

    In the second example, the address is set when you say ptr = &q, so you're OK.

    0 讨论(0)
提交回复
热议问题