Basic question about strcpy with MCUxpresso:

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Basic question about strcpy with MCUxpresso:

2,784 次查看
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

标签 (1)
0 项奖励
回复
6 回复数

2,456 次查看
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

2,457 次查看
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 项奖励
回复

2,457 次查看
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 项奖励
回复

2,457 次查看
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 项奖励
回复

2,457 次查看
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.

2,457 次查看
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 项奖励
回复