Error Message Regarding Char, Const Char, and String

When I try to compile this code, I get the following error message. I'm not exactly sure why. Any suggestions? Thank you in advance for any help!

a.cpp: In function ‘void printBoard(std::string*, int, int)’:
a.cpp:14:21: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]
boardSpace[i][j] = "_";


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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

void printBoard(string boardSpace[], int length, int width)
{
	boardSpace[width][length]; 
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < length; j++)
		{
			boardSpace[i][j] = "_";  
		}
	}

	for (int i = 0; i < width; i++)
	{
		cout << i << "|";
		for (int j = 0; j < length; j++)
		{
			
		}
	}
}


int main () 
{

	int length = 0; 
	int width = 0;
	
	cout << "Width: ";
	cin >> length;
	cout << "Length: ";
	cin >> width;
	cout << endl;

	int **p_p_connectFour; 
	p_p_connectFour = new int*[width]; 

	for (int i = 1; i < width; i++)
	{
		p_p_connectFour[i] = new int[length]; 
	}

return 0; 
}


Also, I am trying to pass a double dimensional string array into a function. However, other error messages show and I do not quite understand why. Any suggestions?

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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std; 

void printBoard(string boardSpace[][length], int width, int length)
{
	for (int i = 0; i < width; i++)
	{
		cout << i << "|";
		for (int j = 0; j < length; j++)
		{
			cout << boardSpace[i][j] << "|"; 
		}
	}
}


int main () 
{

	int length = 0; 
	int width = 0;
	
	cout << "Width: ";
	cin >> length;
	cout << "Length: ";
	cin >> width;
	cout << endl;

	string boardSpace[width][length]; 
	for (int i = 0; i < width; i++)
	{
		for (int j = 0; j < length; j++)
		{
			boardSpace[i][j] = "_";  
		}
	}

	printBoard(boardSpace, width, length); 

	int **p_p_connectFour; 
	p_p_connectFour = new int*[width]; 

	for (int i = 1; i < width; i++)
	{
		p_p_connectFour[i] = new int[length]; 
	}

return 0; 
}
Last edited on
boardSpace[i][j] = "_";
14:21: error: invalid conversion from 'const char*' to 'char'


The boardSpace is an array of std::string and thus the dereferencing
is a call to std::string::operator[]. What type does that function return?

The compiler message says: 'char'

What is the type of "_"?
The compiler message says: 'const char *'

What is the type of '_'?
@keskiverto,

Thank you for your response. I am currently trying to understand your question. What does "dereferencing is a call to std::string::operator[]" mean? I understand that dereferencing is changing the value at a particular memory location. However, I'm not sure how that goes with "std::string::operator" since I don't really know what this statement means.

The type of "_", I believe is a string. Is it not?

And the type of '_' is a character. I think.
If boardSpace is an array of std::string elements, then boardSpace[i] is a std::string.

If foo is a std::string (and boardSpace[i] is a std::string), then what does foo[j] (and boardSpace[i][j]) do?

See http://www.cplusplus.com/reference/string/string/operator%5B%5D/


The type of "_", I believe is a string. Is it not?

No, it is not a std::string object. The compiler claimed that it is const char *, a pointer.

The "_" is a constant string literal. A value stored in special section of program's memory that cannot be modified. That memory location actually occupies two bytes, as if you had written { '_', '\0' } and the pointer has the address of the first byte.

And array of char objects that has null in the last element is called C-string. It is convention that "string functions" of C standard library do use.

Can you now explain this:
1
2
3
4
5
6
7
8
#include <string>

int main() {
  std::string foo( "Hello" );
  foo[1] = 'u';
  foo[3] = "u";
  return 0;
}
@keskiverto

Thank you for responding and being so patient with me. This is super interesting.

foo[j] will return one of two types. A const char or a char, depending on what type the string is initialized with. If it is initialized like so: const string str ("Test String"), then it will return a constant character. If it is initialized like so: string str ("Test String"), then it will return a character. I think.

One syntax that I did not understand on the link that you sent me was that the function will return a const char& or char&. What does "&" imply? Is it giving me the memory location of that const char? I'm assuming so...

No, it is not a std::string object. The compiler claimed that it is const char *, a pointer.

This is a little confusing for two reasons. Why is it returning a const char*? I never qualified my string in the first place to be const string. Further, I thought the function returns the const char & (an address) not const char * (a pointer).

And array of char objects that has null in the last element is called C-string. It is convention that "string functions" of C standard library do use.

Why do they have the null in the last statement? What for? What is the significance of this?

As of now, I am currently unable to explain this since I'm a little bit confused...

I am so sorry for writing so much and thank you so much for being so patient with me! Thank you so much!
Last edited on
the function will return a const char& or char&. What does "&" imply?

A reference. There is a difference between references and pointers. Look them up.

Why do they have the null in the last statement?

Element, not statement. How many elements are on an array? Answer to that is a number and should be stored somewhere somehow. Besides, one does not want to copy arrays by default. It is cheaper to just pass a pointer to the first element to the array and count of elements:
1
2
3
4
5
void foo( char * arr, int n ) {
  for ( int e = 0; e < n; ++e ) {
    std::cout << arr[e];
  }
}


However, not all char are printable and they can be implicitly converted to integers. Booleans have two possible values: false and true, which correrspond to numeric values 0 and non-0. Someone decided to follow the same theme with char: 0 vs all the other characters. With the convention that 0 is end-of-string, one has to pass one less parameter:
1
2
3
4
5
6
void foo( char * arr ) {
  while ( 0 != *arr ) {
    std::cout << *arr;
    ++arr;
  }
}


Why is it returning a const char*?

The it -- "_" -- is a constant C-string literal (and so is "Hello" ). Such literal values are stored is separate part of the binary and loaded into separate memory area when the program loads. The code corresponding to source code, where the literal seems to be, has actually just a number -- the address of the literal.

When you write string str ("Test String"); you actually call the std::string constructor function that takes const char * as parameter. That constructor will copy characters, starting from the address and up to the null to the string's internal array.
@keskiverto

So what is happening here is that, there is some memory that is allocated to foo. And this memory is a special section that cannot be modified (but wasn't it just modified when you set foo[1] = 'u'?). And each of the character, 'h', 'e', 'l', 'l', 'o' is stored in a byte of memory and '0' is the last character stored. And each of the character is considered const char*?

So when I am stating foo[1], I am telling the computer to go to the locaton where 'h' is currently stored and replace it with a 'u'. Now, when I tell the computer foo[3] = "u", this does not work because foo[3] wants a pointer to a const char, but I am giving it a char?

I apologize but I am still a little confused becuase the website stated that the function should return a reference but my compiler is stating that I am dealing with const char*.

Also, one question. Why are you helping me?
The code:
1
2
3
4
5
6
int main() {
  std::string foo( "Hello" );
  foo[1] = 'u';
  foo[3] = "u";
  return 0;
}

Line 2:
Object foo is created in stack memory like any local variable. It is not const.
There is an array in non-modifiable memory that contains { 'H', 'e', 'l', 'l', 'o', 0 }.

The address of 'H' is given to the constructor of foo. The constructor dynamically allocates yet another (modifiable) memory block (from Free Store) and copies characters from the const array.

Line 3:
The foo[1] returns a reference to the second character in the foo's dynamic memory block. The character 'u' is written (assigned) to that position. Thus, the foo has string "Hullo" after this statement.

Line 4:
The foo[1] returns a reference to the fourth character in the foo's dynamic memory block.
The "u", however, is a constant string literal, just like the "Hello" on line 2, and thus the assignment operator has a const char * (the address of the u) as its right operand and a char (reference to the second 'l' within foo) as the left operand.

The assignment operator does not know anything about string, it sees just a char and a pointer. There is no such assignment operator that would assign something from character pointer into a character. Thus, a syntax error.
@keskiverto

I tried to understand everything that you just said. However, I am still having a hard time grasping it. Could you recommend me to some tutorials that might clarify this for me? I know I'm asking alot but if you would, that would be awesome.

I would look for it myself but I'm not even sure what to look for.

Thank you again.
Topic archived. No new replies allowed.