I have some difficulties grasping some concepts. Grateful for help.
Let’s say you have the following piece of code:
int *intPtr; // creates a pointer
int count = 10; // initiates an intiger variable
intptr = &count; // ??
The & operator gives the address to a variable, in this case the integer count. The address is then assigned to the intptr. My question is: Why is intptr = count; not sufficient. I know that count is a variable and intptr is a pointer, but isn´t a variable also just referring to some place in memory?
Hans
count refers to the VALUE of the variable. You don’t want to assign the value of count to intptr, you want to assign the address of count. Thus the & operator is used.
If you do intptr = count, you’d be pointing to memory address 10 in this case, which is sure to be in system memory, not your application memory and you’d crash.