1

I'm trying to create an array of pointers using my professors example in class. I can't seem to get it to work though.

char * ISBN[] = {
    "1-214-02031-3",
    "0-070-21604-5",
    "2-14-241242-4",
    "2-120-12311-x", 
    "0-534-95207-x",
    "2-034-00312-2",
    "1-013-10201-2",
    "2-142-1223",
    "3-001-0000a-4",
};

This is my professors example of declaring an array of pointers of type char. He says this is what we should do for the assignment. Sadly, I'm getting the error - Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" -

I've asked about this before and someone said that char * has been deprecated. But the professor says that this is the declaration that we should use for the assignment. How would I go about trying to figure out how to get this array working? What exactly is this declaration doing?

5
  • 4
    Make it "const char *" and you should be fine. Commented Mar 13, 2020 at 21:59
  • 3
    Add const. Your professor is likely teaching from a 20 year old syllabus. String Literals in C++ are const char * in C (and older C++) char * was allowed (even though the string literal was immutable). No longer. Commented Mar 13, 2020 at 22:00
  • 2
    As horrible as this sounds from a professional standpoint, Find out what compiler the instructor is using and get a copy of that to test your code against. You will have fewer surprises. For example if you make use of the auto keyword which changed behaviours about 10 years back or range-based for which didn't exist. Not to mention if you don fall victim to that blackest of scourges Undefined Behaviour you stand a better chance of the instructor's compiler interpreting it the same way. Commented Mar 13, 2020 at 22:10
  • @DavidC.Rankin "String Literals in C++ are const char *" - more accurately, they are const char[N] which can decay into const char * Commented Mar 14, 2020 at 1:39
  • @RemyLebeau - yes, you are 100% correct. They are arrays that on access are converted to a pointer, subject to the normal exceptions. Commented Mar 14, 2020 at 2:13

2 Answers 2

3
const char * ISBN[] = {
    "1-214-02031-3",
    "0-070-21604-5",
    "2-14-241242-4",
    "2-120-12311-x", 
    "0-534-95207-x",
    "2-034-00312-2",
    "1-013-10201-2",
    "2-142-1223",
    "3-001-0000a-4",
};

I guess I just needed to add const in front of char *. I still got much to learn. Thanks everyone.

Sign up to request clarification or add additional context in comments.

2 Comments

Learning to program isn't a race, it's a journey. Enjoy it, and approach it the same way you would eat a whale -- one byte at a time...
@DavidC.Rankin great. Now you're gonna have Greenpeace after you. Well, at least Rainbow Warriors are easier to spot than Ninja.
1

It is simply because you are trying to assign string literals to char pointer.In c++ string literals are const char pointer because as string literals are stored in read-only memory.Another reason is for optimisation for compilers(storing only one instance of a literal that is repeated many times in the source). So you get multiple pointers to the same memory, instead of each occupying a separate chunk of memory.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.