0

Unable to know why output is 6? In the given C program I am getting output as 6 every time, so I can't say that it is printing Garbage value

#include<stdio.h>
void main()
{
    int const* a=2; 
    printf("%d\n",++(a));
}

I am unable to determine why this C program gives me this answer.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294

5 Answers5

9

When you increment a pointer, it actually increments by the size of the type that it is pointing to.

Here you initialise a pointer to the value 2. You then increment the pointer which, by the above increments the pointer by sizeof(int) or 4 on your system. You then print out this value - 6.

alk
  • 69,737
  • 10
  • 105
  • 255
Sinkingpoint
  • 7,449
  • 2
  • 29
  • 45
  • 1
    It adjusted your wording. However +1 for a straight forward not too theoretical answer. – alk Aug 18 '14 at 15:56
2

You create int pointer and set it to point to address 2. When you try to print, you actually do pointer math (not incrementing value of pointed int). Size of int is 4 bytes, thus, 2 + 4 = 6.

If you want to access value of pointed int, then need to use *:

int *a = malloc(sizeof(int));
*a = 2;
printf("%d\n", ++(*a));

Note, you can't declare a as const, because this code will change value of it. If you want to make a constant pointer (so, you can't change an address it pointer, but still can change data), you need to place const after *:

int * const a = malloc(sizeof(int));
Aleksejs Mjaliks
  • 8,647
  • 6
  • 38
  • 44
  • `int const *a` is OK though (that means the things being pointed to are const, not `a`) – M.M Aug 18 '14 at 07:21
  • @MattMcNabb Actually `int const *a` (or `const int *a`) would declare a pointer whose data cannot be changed. To make constant pointer (but changeable data), it must be declared `int * const a`. http://stackoverflow.com/questions/3247285/const-int-int-const – Aleksejs Mjaliks Aug 18 '14 at 10:49
  • @MattMcNabb Ok, just misunderstanding and language barrier. :) Anyway, I have added more detailed explanation `const` to my answer. – Aleksejs Mjaliks Aug 18 '14 at 10:55
2

This is actually quite easy to debug to see what is going on here. Run this piece of code and look at the values:

#include<stdio.h>
int main()
{
    int const* a=2;

    printf("Size of int %d\n", sizeof(int));
    printf("Size of int const* %d\n", sizeof(int const*));
    printf("Size of a %d\n", sizeof(a));
    printf("Not incremented %d\n", a);
    printf("Incremented %d\n",++(a));

    return 0;
}

I get the output:

Size of int 4
Size of int const* 8
Size of a 8
Not incremented 2
Incremented 6

On a 64-bit machine. Clearly this demonstrates that when you do '++(a)' you are adding 2 to the size of an int. Why should this be the case when you declared

int const* a=2;

?

Well, that is because you are adding 2 to the size of the pointer type rather than the size of the pointer. Change to:

long const* a = 2;

To observe the differences and see this is the case.

This is a quirk of the C/C++ language.

ButchDean
  • 70
  • 6
1

Your program is invalid in more than one way.

Firstly, it is illegal to initialize a pointer with a numerical value (other than constant zero). This

int const* a = 2; 

is not valid C.

Secondly, it is not legal to use %d format specifier to print pointer values. %d is for integers, not for pointers. Which means that this

printf("%d\n",++(a));

produces undefined behavior.

Thirdly, main must return int, not void.

Finally, if your compiler actually swallowed this code and spewed out some sort of executable in return, then one would expect it to print 6. You pointer initialization will "typically" make it point to address 2. Your increment will "typically" increase that address by size of int. Size of int is "typically" 4. 2 + 4 equals 6. %d format specifier applied to pointer will "typically" print decimal representation of the address the pointer is pointing to.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
0

The expression int const* a = 2; is wrong. You could use int const* a = (int const*)2; instead. But there is a question about your aim.

Really Do you want to print the pointer value or the value that a points to?

Be careful that a points to a constant value which is not modifiable.