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


unable know why output 6? in given c program getting output 6 every time, can't printing garbage value

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

i unable determine why c program gives me answer.

this quite easy debug see going on here. run piece of code , @ 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 %d\n", sizeof(a));     printf("not incremented %d\n", a);     printf("incremented %d\n",++(a));      return 0; } 

i output:

size of int 4 size of int const* 8 size of 8 not incremented 2 incremented 6 

on 64-bit machine. demonstrates when '++(a)' adding 2 size of int. why should case when declared

int const* a=2; 

?

well, because adding 2 size of pointer type rather size of pointer. change to:

long const* = 2; 

to observe differences , see case.

this quirk of c/c++ language.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -