Why is there something wrong with my char array?

Evertime I try to run this it keeps saying that there's something wrong with

 
char abc[2] {x, y, z};


Anyone know why?

here's my source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main ()
{  
      char abc[2] {x, y, z};

      cout << abc;

    system ("pause");
  
}
Hey! So, x y and z are variables here. If you want to put a char literal, it should be in single quotes. In other words, like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cstring>

using namespace std;

int main ()
{  
      char abc[3] {'x', 'y', 'z'};

      for(char y : abc) cout << y;

    system ("pause");
  
}

Also, as there are three characters, it should be abc[3], not abc [2]. Infact, as you are initialising the array in the start, you can actually skip the number.
Like :
char abc[] {'x', 'y', 'z'};
If you are wondering why I used for(char y : abc) cout << y; instead of cout << abc;
you can not cout elements of an array like that(as far as I know). You have to use a loop to iterate through each element.
Last edited on
This is perfectly ok:
 
    char abc[3] {'x', 'y', 'z'};


However, if you want to do this:
 
    cout << abc;
then the cout will consider abc is a null-terminated character string. It may therefore output spurious characters until some zero byte is found in memory.

Changing it to
 
    char abc[4] {'x', 'y', 'z'};
will fix the problem, it allows room for the terminating null character. You can explicitly initialise it to zero if you wish, but in this case it isn't necessary.
 
    char abc[4] {'x', 'y', 'z', '\0'};


Now that last example is equivalent to
 
    char abc[4] = "xyz";


What compiler says "something wrong"?

One says:
 In function 'int main()':
10:20: error: 'x' was not declared in this scope
10:23: error: 'y' was not declared in this scope
10:26: error: 'z' was not declared in this scope

Indeed, the x, y, and z are not characters, they are identifiers. Names of variables, but you have not declared such variables.

Were you trying to initialize the array with actual characters? If yes, you should use literal constants:
char abc[2] {'x', 'y', 'z'};

That, however, reveals a new problem. You state that the array has 2 elements. Yet, you try to store 3 values in it. Not possible, or as a compiler could say:
 In function 'int main()':
10:33: error: too many initializers for 'char [2]'


We don't need to write the size for the array, because the compiler can calculate it from the number of initializers.


However, you do have yet another problem on line 12. The output operator does effectively get a memory address of the first element of the array and assumes that the array contains a C-style string. A C-style string has a null character marking the end of the string. Thus, the output reads consecutive characters from memory and prints them, until it encounters a null.

You don't store any null into the array and therefore the output will continue to read beyond the memory reserved for the array. That is undefined behaviour.
Topic archived. No new replies allowed.