kTiwari
kTiwari

Reputation: 1508

What is the the output of the following C programme?

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

Upvotes: 1

Views: 190

Answers (6)

Scooter
Scooter

Reputation: 7059

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
  ma1000 ... ma1015
  _____________________
  |   1|   2|   3|   4|   
  _____________________

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                     *p or arr[0]
  ma2000 .. ma2007      ma1000 .. ma1003
  __________            ________
  |    1000|            |    1 |
  __________            ________

*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

 arr
 ma1000 ... ma1015
 _____________________
 |   2|   2|   3|   4|   
 _____________________

printf then prints the int value at the address "pointed to" by p, which is 2.

  p                     *p or arr[0]

  ma2000 .. ma2007       ma1000 .. ma1003
  ___________           ___________
  |     1000|           |        2|
  ___________           ___________

Upvotes: 1

Abhineet
Abhineet

Reputation: 5409

This is a very basic concept in understanding of pointers.

p = arr ;

The above code will make the pointer " p " to point towards what the " arr " is pointing to. " arr " itself is pointing to the 1st element.

*arr = *(arr + 0) = arr[0] = 1
*(arr + 1) = arr[1] = 2
*(arr + 2) = arr[2] = 3

and so on... So now when you do ++(*arr) that means ++(1) = 2

As p = arr, you can do the remaining replacement and math.

Upvotes: 0

mathematician1975
mathematician1975

Reputation: 21351

You are incrementing the first element in that array by creating another int pointer p that points to the element. The line

 ++*p

increments the value of the object pointed to by p - in this case it is the first element in the array.

Upvotes: 2

Joe
Joe

Reputation: 47749

When you have a pointer int *p, then p means "the memory address p points to and *p means "the contents of the memory address where p points to". ++ means pre-increment, which means increase the value by 1. Since the associativity of unary operators such as * and ++(prefix operators) is right-to-left, and * is closer to p than ++ when traversed right-to-left, * operates before ++. Therefore ++*p means "increment the value pointed to by p", not "increment p then get the value". It is clearer to write ++(*p), but it means the same thing.

The thing to understand is the difference between "increment the pointer", which means point to another value (e.g. ++p) and "increment the value that the pointer points to" (e.g. ++*p).

You can work this out, but that is all the information you need to understand it.

Upvotes: 1

Intrepidd
Intrepidd

Reputation: 20948

Let's take this line :

++*p

This will first dereference the p pointer, so access arr[0], then increment it.

if you print arr[0] now, it will be 2.

Then you print *p that is the same as printing arr[0], it equals 2.

Try replacing the first element by 41, your code will print 42.

Upvotes: 1

Robinson
Robinson

Reputation: 185

The answer should be 2

The reason is ++*p is is actually incrementing the first member in the array by 1.

Upvotes: 2

Related Questions