DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 429--------------------------------------------------
output of program
By: dinesh Date: January 29, 2013, 9:39 am
---------------------------------------------------------
1)
int main()
{
int x = 5;
int * const ptr = &x;
++(*ptr);
printf("%d", x);
getchar();
return 0;
}
2)
int main()
{
int x = 5;
int const * ptr = &x;
++(*ptr);
printf("%d", x);
getchar();
return 0;
}
what is the output and explain it...
#Post#: 435--------------------------------------------------
Re: output of program
By: kpr29 Date: January 29, 2013, 10:16 pm
---------------------------------------------------------
@Majeti: Output:
1) 6 - pointer is constant we cant change the pointer.........
2) Compile error - read only location
#Post#: 436--------------------------------------------------
Re: output of program
By: dinesh Date: January 29, 2013, 10:23 pm
---------------------------------------------------------
int * const ptr —> ptr is constant pointer. You can change the
value at the location pointed by pointer p, but you can not
change p to point to other location.
int const * ptr —> ptr is a pointer to a constant. You can
change ptr to point other variable. But you cannot change the
value pointed by ptr.
Therefore above program works well because we have a constant
pointer and we are not changing ptr to point to any other
location. We are only incrementing value pointed by ptr.
*****************************************************