array initializer must be an initializer list or string literal c

3.7k views Asked by At

I am newbie in C. I do not know why I am getting this error and how to fix it?

void FPS_PRINT_ENROLLED(){
  int checkNum = 0;
  int ID = 0;
  int num_Enroll = 0;
  num_Enroll = Available_ID();
  char strNum[3] = 0;
  itoa(num_Enroll, strNum);
  uint32_t numLen = strlen((char *)strNum);
  UART_send_A3("Number of Stored Prints: ", 25);
  UART_send_A3(&strNum, numLen);
  }

The error message is: array initializer must be an initializer list or string literal

Please see the attached screenshot of the error message. Also, the c file is attached.

Screenshot of error message

By the way, this is in PSoC Creator 4.1 which uses C language.

code file download link through GoogleDrive

1

There are 1 answers

5
lod On BEST ANSWER
char strNum[3] = 0;

This line creates a three character string. You then use it as a string with the function strlen and the uart_send.

As a C string it must be terminated with a null character, so you only have two usable characters. It must also be initialised as a string, the compiler is telling you that you haven't done this correctly.

Try something like one of these lines

char strNum[3] = ""; // Empty string
char strNum[3] = "AB"; // Full string
char strNum[3] = "0"; // String holding the character zero