Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I able to change the contents of const char *ptr?
    primarykey
    data
    text
    <p>I passed a pointer <code>ptr</code> to a function whose prototype takes it as <code>const</code>.</p> <pre><code>foo( const char *str ); </code></pre> <p>Which according to my understanding means that it will not be able to change the contents of <code>ptr</code> passed. Like in the case of <code>foo( const int i )</code>. If <code>foo()</code> tries to chnage the value of <code>i</code>, compiler gives error. <br/> But here I see that it can change the contents of <code>ptr</code> easily. <br/> Please have a look at the following code</p> <pre><code>foo( const char *str ) { strcpy( str, "ABC" ) ; printf( "%s(): %s\n" , __func__ , str ) ; } main() { char ptr[ ] = "Its just to fill the space" ; printf( "%s(): %s\n" , __func__ , ptr ) ; foo( const ptr ) ; printf( "%s(): %s\n" , __func__ , ptr ) ; return; } </code></pre> <p>On compilation, I only get a warning, no error:</p> <p><code>warning: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type</code></p> <p>and when I run it, I get an output instead of <code>Segmentation Fault</code></p> <blockquote> <p>main(): Its just to fill the space<br/> foo(): ABC<br/> main(): ABC<br/></p> </blockquote> <p>Now, my questions is<br/> <strong>1-</strong> What does <code>const char *str</code> in prototype actually means? <br/> Does this mean that function cannot change the contents of <code>str</code>? If that is so then how come the above program changes the value?<br/> <strong>2-</strong> How can I make sure that the contents of the pointer I have passed will not be changed?</p> <p>From "contents of the pointer" in the above stated question, I mean "contents of the memory pointed at by the pointer", not "address contained in the pointer".</p> <h2><strong>Edit</strong></h2> <p>Most replies say that this is because of <code>strcpy</code> and C implicit type conversion. But now I tried this</p> <pre><code>foo( const char *str ) { str = "Tim" ; // strcpy( str, "ABC" ) ; printf( "%s(): %s\n" , __func__ , str ) ; } </code></pre> <p>This time the output is, with no warning from compiler</p> <blockquote> <p>main(): Its just to fill the space<br/> foo(): Tim<br/> main(): Its just to fill the space<br/></p> </blockquote> <p>So apparently, memory pointed to by <code>str</code> is changed to the memory location containing <code>"Tim"</code> while its in <code>foo()</code>. Although I didn't use <code>strcpy()</code> this time.<br/> Is not <code>const</code> supposed to stop this? or my understanding is wrong?</p> <p>To me it seems that even with <code>const</code>, I can change the memory reference and the contents of memory reference too. Then what is the use?</p> <p>Can you give me an example where complier will give me error that I am trying to change a const pointer?</p> <p>Thanks to all of you for your time and effort.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload