I don’t know what the compilar is doing with ++*p;
Can anyone explain me pictorically what is going on inside the memory in this code?
int main()
{
int arr[]={1,2,3,4};
int *p;
p=arr;
++*p;
printf("%d",*p);
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Making up the actual memory addresses and using “ma” for memory address
at memory address starting at 1000 we have 4 continuous 4-byte (sizeof(int) = 4) slots.
each slot contains the integer value given in the array initializer:
arr gives the starting address of the 4 int slots and how many there are.
p holds the address of an integer and refers to one 8-byte slot in memory (assuming we are on a 64-bit system where pointers are 8 bytes – 64 address bits/8bits-per-byte) at location 2000.
After the statement p = arr, p holds the address 1000
*p gives the value at the memory address pointed to by p. p holds memory address 1000 and memory address 1000 contains 1, thus *p results in 1.
++*p says to increment the value of the int “pointed to” by p. p holds memory address 1000 which holds the value 1. The value at address 1000 then goes from 1 to 2
printf then prints the int value at the address “pointed to” by p, which is 2.