I have a question about the syntax in C++ when it comes to declaring arrays. I watched a tutorial where someone used the following code to create the array, but I get errors when running it.
It seems like I can only run the code if I include an =
between the []
and the values of the array. Please explain why he doesn't get compiling issues as I did.
#include <iostream>
using namespace std;
int main(void) {
char vowels[] {'a' ,'e', 'i', 'o', 'u' };
cout << "\nThe first vowel is: " << vowels[0] << endl;
cout << "The last vowel is: " << vowels[4] << endl;
return 0;
}
Output:
main.cpp:10:10: error: definition of variable with array type needs an explicit size or an initializer char vowels[] {'a' ,'e', 'i', 'o', 'u' }; ^ main.cpp:10:18: error: expected ';' at end of declaration char vowels[] {'a' ,'e', 'i', 'o', 'u' }; ^ 2 errors generated.
vowels[1] = 'x';
which doesn't work with your alternative. – François Andrieux Jul 10 '20 at 18:38