Day#15 'C' Coding Challenge

Day#15 'C' Coding Challenge

What is the output of the below program?

#include <stdio.h> void main() { int var1[] = { 2 , 3 , 4 , 5 , 6 , 7 }; int* ptr = &var1; *ptr++; printf("%d", *++ptr); }

----------------------------------------------

The above code tests below scenarios:

1. Pointer to an array declaration
2. Pre and Post increment operator
3. Integer pointer increment

----------------------------------------------

In the above code, ptr is a pointer to the array var1[].
ptr holds the address of first element of var1[].
*ptr points to the value in that pointer address and not the address. When *ptr is post incremented, *ptr is now pointing to the next element in the array which is 3
In the print statement, *ptr is pre incremented and now it points to the next element after 3 and it is 4
So, the output is 4.

Post a Comment

0 Comments