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 9068071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:06:06+00:00 2026-06-16T17:06:06+00:00

Possible Duplicate: why isnt it legal to convert (pointer to pointer to non-const) to

  • 0

Possible Duplicate:
why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)

I have am writing a function which takes a 2D array as input, and does not change the underlying values, and I wanted to explicitly specify this in the function declaration. I am a bit confused about this code (which is obviously just a minimal dummy code to illustrate my question):

int doNotChangeA(int const *a){
    return a[0]+3;
}

int doNotChangeC(int const **c){
    return c[0][0]+3;
}

int main(){

    int *a= new int[1];
    a[0]= 5;
    int b= doNotChangeA(a);
    delete []a;

    int **c= new int*[1];
    c[0]= new int[1];
    c[0][0]= 6;
    int d= doNotChangeC(c);
    delete []c[0];
    delete []c;

    return 0;
}

This code produces a compilation error:

cst.cpp:19: error: invalid conversion from ‘int**’ to ‘const int**’
cst.cpp:19: error:   initializing argument 1 of ‘int doNotChangeC(const int**)’

So, I am a bit confused – why is the first part (non-const 1D array passed to doNotChangeA) allowed and the second (non-const 2D array passed to doNotChangeC) not allowed?

Basically what I want to do is have c be int** which points to non-const values as I want to do whatever I want with it before and after calling doNotChangeC. Of course, I could change doNotChangeC to just take int** instead of int const** and this would “fix” the problem, but I want to explicitly show that doNotChangeC does not change the underlying 2-D array.

Is the right thing to do:

int d= doNotChangeC( const_cast<int const**>(c) );

This compiles fine, but I am confused why is this not required for the first part, i.e. 1-D array (doNotChangeA) ?

Thanks

  • 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-06-16T17:06:07+00:00Added an answer on June 16, 2026 at 5:06 pm

    This would need to be

    int doNotChangeC(int const *const *c){
        return c[0][0]+3;
    }
    

    Without the extra const, your function would be able to do something like this:

    int doNotChangeC(int const **c){
        c[0] = &something_else;
        return c[0][0]+3;
    }
    

    Note that this is different from how C works. In C you would still not be able to pass an int ** to a function that takes a int const *const *, because its rules are simpler.

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

Sidebar

Related Questions

Possible Duplicate: why isnt it legal to convert (pointer to pointer to non-const) to
Possible Duplicate: why isnt it legal to convert (pointer to pointer to non-const) to
Possible Duplicate: Reading and Writing Configuration Files Okay, I have a config file, which
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can a class have a member of its own type, isnt
Possible Duplicate: PhpMailer vs. Swiftmailer? I have always used PHP's built in mail() function
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Simple PHP Mailer Script i run a small website and we have
Lets assume i have a function that takes 32bit integer in, and returns random
Possible Duplicate: Non-web SQL Injection Does someone know of a good example of a

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.