Basic question about strcpy with MCUxpresso:

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Basic question about strcpy with MCUxpresso:

2,017 Views
marianiordan
Contributor II

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

Labels (1)
0 Kudos
Reply
6 Replies

1,689 Views
BlackNight
NXP Employee
NXP Employee

'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

1,690 Views
marianiordan
Contributor II

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

0 Kudos
Reply

1,690 Views
hanisamara
Contributor III

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.

0 Kudos
Reply

1,690 Views
marianiordan
Contributor II

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

0 Kudos
Reply

1,690 Views
converse
Senior Contributor V

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.

1,690 Views
marianiordan
Contributor II

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;
}

0 Kudos
Reply