J
junky_fellow
Hi guys,
Consider the following statements.
const int *ptr; /* ie the contents pointed to by ptr cannot be
changed */
My question is how/who prevents the contents from being modified ?
Is it the C compiler that would give compile time error while trying to
change the contents ? Or is it the implementation that would somehow
prevent the contents from being modified during run time ?
Now consider the following two statements:
const int *ptr; /* line #1 */
int *tmp = ptr; /* line #2 */
*tmp = 100; /* line #3 , Is this valid ? */
Can we change the contents of "tmp" as done is line #3. Will the C
compiler generate warning on line #3. Will this cause some exception
during run time ?
One more question:
const int *ptr = malloc(4 * sizeof(int));
free(ptr); /* gives warning */
While freeing the memory, the compiler generated warning. What is the
reason behind that ? Can't this memory be freed ?
thanks a lot for any help in advance ...
Consider the following statements.
const int *ptr; /* ie the contents pointed to by ptr cannot be
changed */
My question is how/who prevents the contents from being modified ?
Is it the C compiler that would give compile time error while trying to
change the contents ? Or is it the implementation that would somehow
prevent the contents from being modified during run time ?
Now consider the following two statements:
const int *ptr; /* line #1 */
int *tmp = ptr; /* line #2 */
*tmp = 100; /* line #3 , Is this valid ? */
Can we change the contents of "tmp" as done is line #3. Will the C
compiler generate warning on line #3. Will this cause some exception
during run time ?
One more question:
const int *ptr = malloc(4 * sizeof(int));
free(ptr); /* gives warning */
While freeing the memory, the compiler generated warning. What is the
reason behind that ? Can't this memory be freed ?
thanks a lot for any help in advance ...