What is the difference between the * and the & operators in c programming?

前端 未结 3 1299
忘了有多久
忘了有多久 2020-12-29 05:48

I am just making sure I understand this concept correctly. With the * operator, I make a new variable, which is allocated a place in memory. So as to not unnecessarily dupli

3条回答
  •  情深已故
    2020-12-29 06:15

    One uses & to find the address of a variable. So if you have:

    int x = 42;
    

    and (for example) the computer has stored x at address location 5, &x would be 5. Likewise you can store that address in a variable called a pointer:

    int* pointer_to_x = &x; // pointer_to_x has value 5
    

    Once you have a pointer you can dereference it using the * operator to convert it back into the type to which it points:

    int y = *pointer_to_x; // y is assigned the value found at address "pointer_to_x"
                           // which is the address of x. x has value 42, so y will be 42.
    

提交回复
热议问题