cannot convert from 'const char [5]' to 'char *'

Hi there,
I am really new to c++ so be gentle. I am following 'thecherno" lessons on youtube.


He uses the code below but when i use it i get a compiler error.
can anyone help

Thanks

1>c:\users\user\cpp_dev\helloworld\helloworld\arraylesson.cpp(7): error C2440: 'initializing': cannot convert from 'const char [5]' to 'char *'
1>c:\users\user\cpp_dev\helloworld\helloworld\arraylesson.cpp(7): note: Conversion from string literal loses const qualifier (see /Zc:strictStrings)

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <string>

int main() {

	
	char* name = "help";


}
The error indicates that you have tried to turn an object of one type into an object of a different type, and the compiler doesn't like it.

On the right, "help". This is of type const char[5] ; an array of five chars, that may not be changed. They are effectively in read-only memory.

On the left, a char-pointer. The compiler is not happy that you've got something that's const, and you're trying to cast it into something that isn't const. You can convert it to a pointer to a const char, though.

1
2
3
4
5
6
7
#include <iostream>
#include <string>

int main() 
{
	const char* name = "help";
}
You need to make your pointer a pointer to a const char:
 
	const char* name = "help";
I don't know who thecherno is, but if they're showing you outdated, non-standard C++ code from lesson 1, I would suggest finding a different tutorial, or perhaps a book.
https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
thanks all
Topic archived. No new replies allowed.