const char* vs. char*

I do not understand why there are red squiggly lines in line 22 (under "Hello World!" and line 29 (under =). Both indicate - A value of type "const char*" cannot be used to initialize an entity of type "char*".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Soln7_02.cpp
// Add a char* member to the Sample struct in the previous exercise called sptr.
// When you fill in the data for a, dynamically create a string buffer
// initialized with "Hello World!" and make a.sptr point to it.
// Copy a into b.
#include <iostream>                   // For stream input/output

using std::cout;
using std::endl;

struct Sample
{
   int one;
   int two;
   char* sptr;
};

int main()
{
   Sample a;
   Sample b;
   char* s{ "Hello World!" };
   a.one = 1;
   a.two = 2;
   a.sptr = new char[strlen(s)+1];
   strcpy_s(a.sptr, strlen(s)+1, s);

   b.one = b.two = 9999;
   b.sptr = "rubbish";

	cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;

   // b contains values 9999 and a pointer to "rubbish"
   cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;

   b = a;
   cout << "\nAfter copying a to b:" << endl;
   cout << "b=(" << b.one << "," << b.two <<"," << b.sptr << ")" << endl;

   a.sptr[0] = 'H';                           // Change string in a
   cout << "\nAfter modifying the string in a to start with 'H', b has also changed:" << endl;
   cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
   cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;


   // To avoid the two structs pointing to the same string you
   // must create a copy of the string from a, and store its address in b.sptr
   b.one = a.one;
   b.two = a.two;
   b.sptr = new char[strlen(a.sptr)+1];
   strcpy_s(b.sptr, strlen(a.sptr)+1, a.sptr);

   cout << "\nAfter copying members of a to b, replicating the string:" << endl;
   cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
   cout << "b=(" << b.one << "," << b.two <<"," << b.sptr << ")" << endl;

   a.sptr[0] = 'h';
   cout << "\nAfter modifying the string in a once more to start with 'h', b has not changed:" << endl;
   cout << "a=(" << a.one << "," << a.two << "," << a.sptr << ")" << endl;
   cout << "b=(" << b.one << "," << b.two << "," << b.sptr << ")" << endl;

	system("pause");
	return 0;
}
char* s{ "Hello World!" };

this isn't right.
try
char *s = "Hello World";

The type of the string literal "Hello World!" is 'array of 13 const char'
http://coliru.stacked-crooked.com/a/dd15918d9031d683

Change it to:
1
2
// char* s{ "Hello World!" };
const char* s{ "Hello World!" }; // *** note: const 

Thanks jonnin, but it did not work. The red squiggly line is now under the assignment symbol. The brackets are used to initialized char* s, but char*s must be declared const as JL Borges indicated.

Now what about line 29: b.pstr = "rubbish"; where the red squiggly line is under the assignment symbol.
Last edited on
> Now what about line 29: where the red squiggly line is under the assignment symbol.

Q1: What is the type of b.sptr ?
Q2: What is the type of "rubbish" ?
Q1: Not sure. But, I think it is a char*.
Q2: A const char*

But, if I redefine line 29 to const b.sptr = "rubbish"; it still results in a error.
> I think it is a char*.

Yes.


> A const char*

No. Type of "rubbish" is 'array of 8 const char'



> But, if I redefine line 29 ...

You are not the declaring a new variable; you are assigning to an existing object.

This should be ok:
1
2
3
// b.sptr = "rubbish";
char rubbish[] = "rubbish" ;
b.sptr = rubbish;
Thank you JL. It works.
Registered users can post here. Sign in or register to post.