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?


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:

  1. char arr[6] = {'h', 'e', 'l', 'l', 'o', '\0'}; // initializer list
  2. 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';
  • 1
    If you're initialising an array to a string, you don't have to specify the array size, just doing char str[] = "hello" works. – CodeFactz Jun 12 '14 at 17:24
  • Thanks, @CodeFactz! I wasn't really sure about this. Just tried it and it works perfectly! Edited my answer! – kzidane Jun 12 '14 at 22:12
  • @Kareem - What if you want to create a 2D array using GetString? How do you do that? – Karthik Sampath Sep 14 '15 at 10:13
  • @KarthikSampath so you wanna create an array of strings and use GetString to fill it? – kzidane 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.

  • You've to give a type to text and len. And no need to declare len outside of the loop since we won't need it except within the loop. isalpha() returns true if the char passed to it is an alphabetical char and false if it's otherwise. isupper() returns true if the char passed to it is an uppercase letter and false if it's otherwise. – kzidane Jun 11 '14 at 20:06
  • @KareemMesbah int len and string text have been defined outside of the main function in my code. But still thanks for bringing it to the notice for future references. – Vatsal Jun 11 '14 at 20:23
  • To use strlen you need to have the string.h library included. – chrisgallardo Nov 29 '20 at 18:13

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



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... :-)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .