Basic question about strcpy: I am using MCUxpresso IDE 10.0 for KL26z128 processor, and I am getting a compile error on following piece of code - need some help with it.
376: char LCD_TXT[2][21];
377: strcpy(LCD_TXT[0], 'def');
378: strcpy(LCD_TXT[1], 'tuv');
Description Resource Path Location Type
expected declaration specifiers or '...' before '\x646566' main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected declaration specifiers or '...' before '\x747576' main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
expected declaration specifiers or '...' before 'LCD_TXT' main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected declaration specifiers or '...' before 'LCD_TXT' main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
make: *** [source/main.o] Error 1 MKL26Z128xxx4_Project C/C++ Problem
recipe for target 'source/main.o' failed subdir.mk /MKL26Z128xxx4_Project/Debug/source line 18 C/C++ Problem
if changing as below (and corrected is as per Eric's comments - 12/21), output is slightly different, though still failing:
376: char LCD_TXT[2][21];
377: strcpy(char LCD_TXT[0], char "def");
378: strcpy(char LCD_TXT[1], char "tuv");
Description Resource Path Location Type
expected ';', ',' or ')' before string constant main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected ';', ',' or ')' before string constant main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
make: *** [source/main.o] Error 1 MKL26Z128xxx4_Project C/C++ Problem
recipe for target 'source/main.o' failed subdir.mk /MKL26Z128xxx4_Project/Debug/source line 18 C/C++ Problem
'def' is a multi-byte character, not a string (or array of char).
You have to use double quotes:
strcpy(LCD_TXT[0], "def");
strcpy(LCD_TXT[1], "tuv");
I hope this helps,
Erich
Eric, thanks for the correction, though still does not solve the error:
strcpy(char LCD_TXT[0], char "def");
strcpy(char LCD_TXT[1], char "tuv");
Description Resource Path Location Type
expected ';', ',' or ')' before string constant main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected ';', ',' or ')' before string constant main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
make: *** [source/main.o] Error 1 MKL26Z128xxx4_Project C/C++ Problem
recipe for target 'source/main.o' failed subdir.mk /MKL26Z128xxx4_Project/Debug/source line 18 C/C++ Problem
Hello,
why you write char befor LCD_TXT[0] in the strcpy statement?
try this: strcpy(LCD_TXT[0], (char*) "def"); or this: strcpy(LCD_TXT[0], "def"); both of them showld works.
Hani,
Thanks for taking the time. if I don't do char before destination, it complains about it missing (see below). I tried strcpy(LCD_TXT[0], "def") in a different environment (both Arduino gcc and Qt) - everything is fine. however in MCUxpresso I get the error above; below you have the errors for both suggestions you mentioned:
376: char LCD_TXT[2][21];
377: strcpy(LCD_TXT[0], (char*) "def");
378: strcpy(LCD_TXT[1], "tuv");
expected declaration specifiers or '...' before '(' token main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected declaration specifiers or '...' before 'LCD_TXT' main.c /MKL26Z128xxx4_Project/source line 377 C/C++ Problem
expected declaration specifiers or '...' before 'LCD_TXT' main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
expected declaration specifiers or '...' before string constant main.c /MKL26Z128xxx4_Project/source line 378 C/C++ Problem
Looks like it is more likely to be how strcpy is defined. How have you defined strcpy? Have you #include <string.h>?
As an example, this snippet compiles perfectly:
#include <string.h>
void test() {
char LCD_TXT[2][21];
strcpy( LCD_TXT[0], "def");
strcpy( LCD_TXT[1], "tuv");
}
Compare this with your failing example, and see if you can fix it. If not, post a compilable example that reproduces your problem.
Thanks to all that took their time to read and answer (especially Con Verse for putting the code in the context). The problem I had was trying to use the code outside any function's body, just below global declarations. Now it works as expected.
int initLCD(void) {
char LCD_TXT[2][21];
strcpy(LCD_TXT[0], "abc");
strcpy(LCD_TXT[1], "tuv");
return 0;
}