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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:24:52+00:00 2026-05-16T00:24:52+00:00

Why can I not do this: char* p = new char[10]; void SetString(char *

  • 0

Why can I not do this:

char* p = new char[10];

void SetString(char * const str)
{
    p = str;
}


SetString("Hello");

I have a const pointer to a char, why can I not assign the const pointer to another pointer?

It just seems illogical, as by assigning it to another pointer, you are not essentially violating the const-ness of the char pointer. Or are you?

EDIT:
When I compile this it says “error C2440: ‘=’ : cannot convert from ‘char *const *__w64 ‘ to ‘char *'”

(I’m attempting to understand a concept from a book I’m reading. Just cannot get the code to compile.

CODE:

int _tmain(int argc, _TCHAR* argv[])
{

    MyString *strg = new MyString(10);
    strg->SetString("Hello, ");

    MyString *secondstr = new MyString(7);
    secondstr->SetString("Tony");

    strg->concat(*secondstr, *strg);

}

CPP FILE:

#include "MyStringClass.h"
#include <string.h>
#include "stdafx.h"

#include "MyStringClass.h"

void MyString::concat(MyString& a, MyString& b)
{
    len = a.len + b.len;
    s = new char[len + 1];
    strcpy(s, a.s);
    strcat(s, b.s);
    delete [] s; 

}

void MyString::SetString(char * const str)
{
    s = str;
}

MyString::MyString(int n)
{
    s = new char[n+1];
    s[n+1] = '\0';
    len = n;
}

HEADER FILE:

#include <string.h>
#include <stdio.h>

class MyString
{
private:
    char* s;
    int len;
public:
    MyString(int n = 80);

    void SetString (char * const str);

    void concat (MyString& a, MyString& b);
};
  • 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-16T00:24:53+00:00Added an answer on May 16, 2026 at 12:24 am

    There is difference between constant pointer and pointer to constant. Constant pointer is a pointer (a number – memory address) that cannot be changed – it always point to the same object given via initialization:

    int * const const_pointer = &some_int_var; // will be always pointing to this var
    const_pointer = &some_other_var; // illegal - cannot change the pointer
    *const_pointer = 2; // legal, the pointer is a pointer to non-const
    

    Pointer to constant is a pointer whose pointed value cannot be changed:

    const int * pointer_to_const = &some_int_var; // doesn't have to be always pointing to this var
    pointer = &some_other_var; // legal, it's not a constant pointer and we can change it
    *pointer = 2; // illegal, pointed value cannot be changed
    

    You can always assign constant to variable i.e. const pointer to non-const pointer (a). You can cast pointer to non-const to a pointer to const (b). But you cannot cast pointer to const to a pointer to non-const (c):

    int * pointer;
    int * const const_pointer = &var;
    const int * pointer_to_const;
    
    /* a */
    pointer = const_pointer; // OK, no cast (same type)
    
    /* b */
    pointer_to_const = pointer; // OK, casting 'int*' to 'const int*'
    
    /* c */
    pointer = pointer_to_const; // Illegal, casting 'const int*' to 'int*'
    

    [EDIT] Below, this is not standard c++. However, this is common.[/EDIT]

    String literal

    "Hello"
    

    is converted to constant pointer to const (const char * const):

    char *pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
    char * const const_pointer = "Hello"; // Illegal, cannot cast 'const char*' to 'char*'
    const char * pointer_to_const = "Hello"; // OK, we can assign a constant to a variable of the same type (and the type is 'const char*')
    "Hello" = pointer_to_const; // Illegal cannot re-assign a constant
    

    In above examples the second is your case. You tried to initialize pointer-to-non-const with a pointer-to-const when passing string literal as argument of your function. No matter if these pointers are constants or not, it’s matter what do they point to.

    Summary:
    1) If you cast a pointer of some type to a pointer of another type, you cannot cast pointer-to-const to pointer-to-non-const.
    2) If you have constant pointer, the same rules applies as to other constants – you can assign a constant to a variable but you cannot assign a variable to a constant (except initializing it).

    // EDIT
    As GMan pointed out, the C++98 standard (§4.2/2) allows to implicitly cast string literals (which are constant char arrays) to a non-const char pointer. This is because of backward compatibility (in C language there are no constants).

    Of course such a conversion can lead to mistakes and compilers will violate the rule and show an error. However, GCC in compatibility mode shows only a warning.

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

Sidebar

Related Questions

foo\r\nbar.replace(/(foo).+/m, bar) Hello. I can not understand why this code does not replace foo
Any reasons why this can not be standard behavior of free() ? multiple pointers
I suppose this is not so hard but I can not get it. For
Can not parse json using this var res = eval('(' + response + ')');
I can not get to make this comparison in this simple code error ..
there's this interesting problem i can not solve myself. I will be very glad,
The error shows this code can not rollback: class AddCountToTag < ActiveRecord::Migration def change
I am not professional programmer so i can not be sure about this.How many
i use this method before 1 time. but Now i can not get video
This should be pretty simple, still I can not see what/how something could be

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.