Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

function(char message []) not compiling with function("Text");

I am having an issue with trying to send a char[ ] message from main to my function.

InputNum(char msg []) { cout << msg; }

then called as

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

int main() { InputNum num ("Enter a Number"); }

The above throws an error from the g++ compiler:

ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]

So, I am sending a String in main as a char[ ]. But I’m not sure why that is a problem. I was under the illusion that a char[ ] IS a String, which I guess is incorrect. Any general advice on what and how to fix it, I would appreciate greatly.

Also, code is inspired by p.44 "C++ In Action" by Bartosz Milewski (2001).

>Solution :

You are trying (unknowingly) to make a string literal writable. C++ doesnt like this at all.

You have to do

InputNum(const char msg []) {
   cout << msg;
}

this promises the compiler that you wont try to change the conents of ‘msg’

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading