Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 901501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:33:51+00:00 2026-05-15T15:33:51+00:00

I passed a pointer ptr to a function whose prototype takes it as const

  • 0

I passed a pointer ptr to a function whose prototype takes it as const.

foo( const char  *str );

Which according to my understanding means that it will not be able to change the contents of ptr passed. Like in the case of foo( const int i ). If foo() tries to chnage the value of i, compiler gives error.
But here I see that it can change the contents of ptr easily.
Please have a look at the following 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;
}

On compilation, I only get a warning, no error:

warning: passing argument 1 of ‘strcpy’ discards qualifiers from pointer target type

and when I run it, I get an output instead of Segmentation Fault

main(): Its just to fill the space
foo(): ABC
main(): ABC

Now, my questions is
1- What does const char *str in prototype actually means?
Does this mean that function cannot change the contents of str? If that is so then how come the above program changes the value?
2- How can I make sure that the contents of the pointer I have passed will not be changed?

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”.

Edit

Most replies say that this is because of strcpy and C implicit type conversion. But now I tried this

foo( const char  *str )
{
        str = "Tim" ;
//      strcpy( str, "ABC" ) ;
        printf( "%s(): %s\n" , __func__ , str ) ;
}

This time the output is, with no warning from compiler

main(): Its just to fill the space
foo(): Tim
main(): Its just to fill the space

So apparently, memory pointed to by str is changed to the memory location containing "Tim" while its in foo(). Although I didn’t use strcpy() this time.
Is not const supposed to stop this? or my understanding is wrong?

To me it seems that even with const, I can change the memory reference and the contents of memory reference too. Then what is the use?

Can you give me an example where complier will give me error that I am trying to change a const pointer?

Thanks to all of you for your time and effort.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:33:51+00:00Added an answer on May 15, 2026 at 3:33 pm

    Your understanding is correct, const char* is a contract that means you can’t change memory through this particular pointer.

    The problem is that C is very lax with type conversions. strcpy takes a pointer to non-const char, and it is implicitly converted from const char* to char* (as compiler helpfully tells you). You could as easily pass an integer instead of pointer. As a result, your function can’t change content pointed by ptr, but strcpy can, because it sees a non-const pointer. You don’t get a crash, because in your case, the pointer points to an actual buffer of sufficient size, not a read-only string literal.

    To avoid this, look for compiler warnings, or compile, for example, with -Wall -Werror (if you are using gcc).

    This behaviour is specific to C. C++, for example, does not allow that, and requires an explicit cast (C-style cast or a const_cast) to strip const qualifier, as you would reasonably expect.

    Answer to the extended question

    You are assigning a string literal into a non-const char, which, unfortunately, is legal in C and even C++! It is implicitly converted to char*, even though writing through this pointer will now result in undefined behaviour. It is a deprecated feature, and only C++0x so far does not allow this to happen.

    With that said, In order to stop changing the pointer itself, you have to declare it as const pointer to char (char *const). Or, if you want to make it that both the contents pointed by it and the pointer itself don’t change, use a const pointer to const char (const char * const).

    Examples:

    void foo (
            char *a,
            const char *b,
            char *const c,
            const char *const d)
        {
        char buf[10];
        a = buf; /* OK, changing the pointer */
        *a = 'a'; /* OK, changing contents pointed by pointer */
    
        b = buf; /* OK, changing the pointer */
        *b = 'b'; /* error, changing contents pointed by pointer */
    
        c = buf; /* error, changing pointer */
        *c = 'c'; /* OK, changing contents pointed by pointer */
    
        d = buf; /* error, changing pointer */
        *d = 'd'; /* error, changing contents pointed by pointer */
    }
    

    For all error lines GCC gives me “error: assignment of read-only location”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to call a function which passed as function pointer with no argument,
The const modifier in C++ before star means that using this pointer the value
As I know, when a pointer is passed into a function, it becomes merely
I have a class template which can be passed a Class or Class pointer
I'm using boost python. I've exported some function which takes class CL_DomElement in arguments.
in the following snippet I wish the function accept a double pointer(2D array) which
I'm using a unique_ptr to pass a const wchar_t pointer to a function. In
I have a function called ReversedIPAddressString which takes IPAddress as a TCHAR* and then
Does glUniformMatrix4fv instantly copy data pointed to by the passed in pointer? If not
I need help with malloc() inside another function . I'm passing a pointer and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.