Given this array:
char p[90] = GetString();
I get this error when compiling:
error: array initializer must be an initializer list or string literal
How to initialize this array properly?
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.
Sign up to join this communityGiven this array:
char p[90] = GetString();
I get this error when compiling:
error: array initializer must be an initializer list or string literal
How to initialize this array properly?
First, when declaring a char
array, you've to specify its size within the brackets like:
char arr[10];
except when you're initializing it with a string literal.
And since we don't know the number of characters the user will enter beforehand we may consider that GetString()
takes care of that and returns a string
(a char *
). You may just declare a string
and assign GetString()
to it like that:
string s = GetString();
then access the contents of that string
like you'd do with a char
array. For example, if I wanna print out the first char
in that string
I may have something like this:
printf("%c\n", s[0]);
Second, assuming you're using a char
array for any reason, you don't have to cast the char
you're accessing to a char
since it's already a char
! So considering this piece of code:
char arr[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
printf("%c\n", arr[0]); // no need to say (char) arr[0]
Lastly, you may print out the ciphered chars directly without storing them since we don't need them after printing them out.
Edit: the error
array initializer must be an initializer list or string literal
means that when you declare an array, you have to initialize it using one of the following methods:
char arr[6] = {'h', 'e', 'l', 'l', 'o', '\0'}; // initializer list
char arr[6] = "hello"; // string literal
char arr[] = "hello"; // also a string literal
A third way to initialize an array would be initializing its indices individually like that:
char arr[6];
arr[0] = 'h';
arr[1] = 'e';
arr[2] = 'l';
arr[3] = 'l';
arr[4] = 'o';
arr[5] = '\0';
GetString
to fill it?
– Kareem♦
Sep 14 '15 at 17:16
You need not go through all this trouble. Just writing
text = GetString();
len = strlen(text);
will also do your job.
Then you can continue as you are already doing by
for (i = 0; i < len; i++)
{
if ( isalpha(text[i]) != 0 )
{
if ( isupper(text[i]) != 0)
{
// do something
}
}
}
Also note that isalpha returns 1024 if alphabet is passed and 0 otherwise and isupper returns 256 for uppercase alphabets and zero otherwise. Please make sure that you check the values of isalpha and isupper once by printing them before proceeding with using them in your loop.
Don't mean to be rude, but why don't you read the specifications, trying to understand what they ask you?
The first error line of the compiler means: look at line 26, your error is there. Showing your partial source code without the line numbering is useless.
If you don't get the sense of the message, try googling the string "error: array initializer must be an initializer list or string literal" and read how other people like you resolved the issue.
Then try looking for the difference between a declaration like
charp[90]
and
char* p
There is your answer.
Finally, if you have no idea of what to do with those arrrays, give a look here.
Google is your friend. we are your friends too, but you can't have your Pset done by others if you don't even try to understand what you are assking... :-)
Luigi