0

Upon research I actually found the answer to my previous question. Why the heck is the computer telling me I can't define the elements of my array.

#include <iostream>


 using namespace std;
 int x;
 int y;

 int main(){

 char DuplicateDeleter[6];
    //we must define each element
DuplicateDeleter[0] = 'a';
DuplicateDeleter[1] = 'b';
DuplicateDeleter[2] = 'c';
DuplicateDeleter[3] = 'd';
DuplicateDeleter[4] = 'e';
DuplicateDeleter[5] = 'f';
 }

The answer was that I had to use 'a' over "a", and my question is what's the difference between these two? I was under the suspicion that they pretty much did the same thing. What is "x" telling the computer that 'x' isn't?

error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] - source

Thomas
  • 2,622
  • 1
  • 9
  • 16

2 Answers2

1

"x" is NULL terminated character array. It is having 2 characters i.e. {'a', '\0'}. 'x' is a single character.

You can verify it by following code snippet. See it working here:

int main() {
    cout<<"sizeof(\"x\") = " <<sizeof("x") <<endl<<"sizeof('x') = " <<sizeof('x') <<endl;

    cout<<"Content of \"x\" are(in Hexadecimal): "<<endl;
    char x[] = "x";
    cout.setf(ios::hex, ios::basefield);
    for(int i=0; i<sizeof("x"); i++)
         cout<<"\t "<<(int)x[i] <<endl;
    return 0;
}

Output:

sizeof("x") = 2
sizeof('x') = 1
Content of "x" are(in Hexadecimal): 
     78
     0

You can see more information here and here

cse
  • 4,066
  • 2
  • 20
  • 37
1

'a' is the character "a". Just one character, not a string.

"a" is the an array of two chars; it's equivalent to this: char a[2] = {'a', '\0'}; The extra character (\0) is the null-terminator, since raw strings in C/C++ don't maintain their length explicitly.

Stephen Newell
  • 7,330
  • 1
  • 24
  • 28