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

  • Home
  • SEARCH
  • 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 6728409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:10:00+00:00 2026-05-26T10:10:00+00:00

Whey we cannot Convert pointer to a character ->TO-> a reference to a pointer

  • 0

Whey we cannot Convert pointer to a character ->TO-> a reference to a pointer to a constant character

I am interested in knowing the reason of syntax error when we call foo_ptr. When foo_char is allowed why not foo_ptr.

[Update 1.] I would be happy in knowing the reason that foo_char() is working, why foo_ptr() is not working .. What happens when pointer come in the picture.


[Update 2.]
Didnt work in Dev C++ compiler version 4.9.9.2 too ..

//code
//OS : Win XP
//Env: VC++ 2008 

//NOT ALLOWED
void foo_ptr(const char * & ptr) //reference to a pointer to a constant character         
{         
        return;         
}        


//allowed        
void foo_char(const char & p_Char) //reference to a constant character        
{         
        return;        
}        

int main()        
{        
        char ch = 'd';        
        char *ptr =  "anu";        

        foo_char(ch);         
        foo_ptr(ptr); //NOT ALLOWED syntax error, vc++, 2008        

        return 0;        
}        
  • 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-26T10:10:01+00:00Added an answer on May 26, 2026 at 10:10 am

    Revised with more examples:
    Raymond Chen provides the correct answer. By passing a non const pointer (char *) as reference parameter of a const pointer (foo_ptr(const char * &param)) you risk returning a const pointer type (const char *) and the compiler won’t allow you to do that.

    Here’s Raymond Chen‘s example of that, but I tried to explain how things would go wrong if it compiled by adding additional comments and code:

    void foo_ptr(const char * & ptr)
    {         
        //Valid assignment, and the char * is now pointing to a const
        //array of "readonlystring"
        ptr = "readonlystring";
    }   
    
    ...
    //inside main
    char *ptr = malloc(10*sizeof(char));
    //See you can edit ptr, it's not const.
    ptr[0] = 'a';
    ptr[1] = 'b';
    //this should not compile, but lets assume it did..
    foo_ptr(ptr);
    //Oh no, now ptr[0] is 'r' inside of constant memory,
    //but now since ptr isn't declared const here I can overwrite it!
    //But luckily most (all?) compilers actually fail to compile this code.
    ptr[0] = 'b';
    

    But if you change your parameter so you can’t affect the value that the pointer points to then the compiler will let you past in a non-const because there is no chance a const valued pointer is returned.

    By placing the keyword const AFTER the * in your parameter deceleration you do just that. That means change:

    void foo_ptr(const char * & ptr)
    

    to

    void foo_ptr(const char * const & ptr)
    

    and your compiler will be happy.

    Now you would not be able to do something like ptr = "readonlystring" in the above example because that would never compile now. Based on your question that should be OK because you would not be able to do the assignment to a const char & in your original example.

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

Sidebar

Related Questions

Let me start by saying, I also get the same error whey defining __init__
Whey does this evaluate to true? <?php $val2=0; //outputs that is an error123 if($val2=='error123'){
i just cant do it, dont kno whey. How can I get the value
I got this message when I tried to log off from a site: You
Is it possible to avoid having 'NULL' stings on the client when binding JSON
I am about to build an SQL database and I need to get it
You can see lots of briliant ideas in Open source projects. How authors found
I have the interface with many controls which I need to set, the problem
Background: On a recent website, I'm using a jQuery driven dropdown menu in addition
According to this : http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f9f.html Quote: An untyped variable is not the same as

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.