I am learning C++ from a textbook (C++: A Beginnners Guide, Second Edition, Herbert Schildt). The following program code is from the book, but results in error, can someone please explain to me why this is not allowed?
The aim is to demonstrate a pointer as a parameter:
#include <iostream>
using namespace std;
char *get_substr(char *sub, char *str); //function prototype
int main()
{
char *substr;
substr = get_substr("three", "one two three four");
cout << "substring found: " << substr;
return 0;
}
I'll not list the function body because it goes as you would expect, but even if it just returns zero, this results in the following error: E0167 argument of type "const char *" is incompatible with parameter of type "char *", referencing the function call. My understanding is a string is basically an array of char in C anyway, why is this not allowed, and what is a suitable alternative? Thank you in advance.