-2

I'm trying to declare a global 6x6 char array in c++ with the entire array initialised with the letter 'I'.

 char result[] = new char[6][6];
 result={{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},
{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I'}};

error obtained : 1. error: array initializer must be an initializer list or string literal 2. C++ requires a type specifier for all declarations. This error goes away if I initialise in main().

Query solved.

Sim
  • 27
  • 4

3 Answers3

1

Did you try 'I' rather than just? Something like

char result[] = new char[6][6];
result={{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I'},{'I','I','I','I','I','I}};
Barath
  • 21
  • 1
  • 6
  • Even though your answer is correct, your lack of certainty makes it seem like you've prematurely posted an answer in the hopes of grabbing free reputation. – byxor Mar 04 '17 at 14:14
  • Yes this removed the third error thankyou – Sim Mar 04 '17 at 14:16
  • @BrandonIbbotson That's true. I'm a new stackoverflow user and I don't even have the reputation to down vote this question. Please down vote this question with your maturity, ignore you already did that. – Barath Mar 04 '17 at 14:18
  • @Barath I agree the third error was due to my silliness but the first two are something I don't understand even after searching, that's why I posted the question after a lot of trial and error by myself – Sim Mar 04 '17 at 14:20
  • My comment was a little bit rude, maybe I should retract it and give more helpful advice. Try to be more assertive with your answers. A confident answer is _usually_ a good one. – byxor Mar 04 '17 at 14:21
1

You do definition and initialization in different steps. First of all you can't have general statements in the global scopes (like an assignment). Secondly you can't really assign to an array. Thirdly you don't have an array and try to change pointers.

The error is becaise of the first reason.

The simple solution is to use actual arrays, and do definition and initialization in a single step:

char result[6][6] = {
    { 'I', 'I', 'I', 'I', 'I', 'I' },
    { 'I', 'I', 'I', 'I', 'I', 'I' },
    ....
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

In order to use compile-time constant char values you need to surround the character with a pair of commas: 'I'. Your program is trying to place the value of a variable or constant named I into the array.

  • Thank you so much, this was pretty dumb of me. However the first error persists. I have edited question accordingly. – Sim Mar 04 '17 at 14:15
  • Operator new returns a pointer to char instead of an array type. Since you're using a statically-sized array, you're better off using static initialization: `char result[6][6] { {'I','I','I','I','I','I'}, ... };` – Alexander Ivanov Mar 04 '17 at 14:23