• Register
1 vote


I am getting the following error :

error: array initializer must be an initializer list or string literal

For the array;

char x[80] = GetString();  

Can any body explain or help?

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

1 vote


First, when declaring a char array, you've to specify its size within the brackets like:

char arr[10]; or char arr[20];
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 which 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 x = 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", x[0]);
by 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

And a secoend way to initialize is:

char arr[7];

arr[0] = 'k';
arr[1] = 'a';
arr[2] = 'm';
arr[3] = 'r';
arr[4] = 'u';
arr[5] = 'l';
arr[6] = '\0';


11 6 4
34,950 points

Related questions

1 vote
1 answer 37 views
Problem: I am beginner in pyhton prgramming language, I tried to convert string array to int array using python so I wrote a code which contains- testing_list = ['4', '2', '3', '9', '5'] And my code is: testing_list = ['4', '2', '3', '9', '5 ... list is : str(testing_list) Converted list is : str(testing_list) why am I not getting the converted result and what is the way to get the acurate result?
asked Mar 29, 2020 LizzyM 6.1k points
0 votes
1 answer 19 views
I am new to python programming.. I was trying to check if a string is empty or whitespace in a code : Mystr= "" Yourstr = " " print ("This string is empty : ", end = "") if(not Yourstr): print ("Yes") print ("This string is empty : ", end = "") if(not Mystr): print ("No") It’s output is : This string is empty : This string is empty : No Its not giving me actual result what can I do now?
asked Mar 16, 2020 LizzyM 6.1k points
0 votes
1 answer 11 views
Problem: On the last line, I got this error: builtins.TypeError: must be str, not bytes File "C:\PythonExamples\XmlReportGeneratorExample.py", line 29, in <module> doc.write(outFile) File "c:\Python32\Lib\site-packages\lxml\etree.pyd", line 1853, in lxml.etree._ElementTree.write (src/ ... etree.c:89527) I've installed Python 3.2, and I've installed lxml-2.3.win32-py3.2.exe. On Python 2.7 it works.
asked Jan 9 Mashhoodch 9.1k points
1 vote
1 answer 21 views
Problem: I was tring to find the length of a string in python. So I wrote a code: string = 0 for c in "popular": string+==1 print (string) which gives following error; File "ab.py", line 3 string+==1 ^ SyntaxError: invalid syntax Is this the right way to do this or there are other waysto find this out?
asked Mar 28, 2020 LizzyM 6.1k points
0 votes
1 answer 112 views
I am trying to figure out why this error showing in my case and searching several website didn't get the desired result. In this website can someone can someone show me correct way to solve this ValueError: Must pass DataFrame with boolean values only.
asked Sep 14, 2020 Daniel Anderson 4k points
1 vote
2 answers 2K views
Problem: I was trying to insert data from one of my dictionaries into my database for that I wrote the code: def all_db_inserts(dbinformation): try: rows = dbinfo['datarows'] for row in rows: field_names = ",".join(["'{0}'".format(x) for x in row.keys()] ... this error message when I try to run this(using sample data): TypeError: sequence item 0: expected string, int found Can anyone help me out?
asked Apr 5, 2020 LizzyM 6.1k points
1 vote
3 answers 649 views
Problem : I'm a newbie with Python's arrays and I don't understand how I can avoid below error: python typeerror: list indices must be integers or slices, not str
asked Oct 21, 2019 peterlaw 6.9k points
1 vote
1 answer 35 views
Problem: I am new in pyhton programing language . I was trying to check if two strings are anagrams python. But I fail repitedly. The code I wrote to check if two strings are anagrams is: def IsAnagram(str1,str2): str1 = input("Enter your first string : ") ... , line 4 str1 = input("Enter your first string : ") ^ IndentationError: expected an indented block What is the problem can anyone tell?
asked Apr 2, 2020 LizzyM 6.1k points