constant qualifier

this function is giving value 10 after modification in main but when printed in fun(), it is giving modified value… how is this happening??

#include<stdio.h>

#include<conio.h>

void fun(const int *s)
{
printf("In this function %d\n",*s);
}

int main()

{

const int a = 10;

int *p = (int*)&a;

printf("before modification a=%d\n",a);

*(int*)&a = 12;

printf("after modification a=%d\n",a);
fun(&a);
getch();

return 0;

}
1 Like

This is not an answer, just observation.

It seems, that you somehow change value of p

#include<stdio.h>

void fun(const int *s)
{
	printf("In this function %d\n",*s);
}

int main()
{
	const int a = 10;
	int *p = (int*)&a; // const int* to int*
	printf("before modification a=%d\n",a);

	*(int*)&a = 12;

	printf("after modification a=%d\n",a);
	printf("after modification p=%d\n",*p);
	fun(&a);
	//getch();

	return 0;
}

C/C++ is magical…

My gcc version

gcc version 4.5.3 (GCC)

Here are IdeOne results:

//