error:对于字符,不能作为strcpy、strcat函数的参数

本文详细解析了C++中字符串操作中的常见错误,重点讲解了使用`_tcslen`初始化字符串和避免将变量作为常量指针传递的技巧。通过实例演示了如何正确地使用字符串连接函数,确保程序的稳定性和效率。

char szSelected;


LPTSTR lpCmd;


_tcscat(lpCmd,szSelected);是错误的,因为第二个参数必须是常指针,而不是字符变量。


 lpCmd[_tcslen(lpCmd)] = szSelected;才是正确的。


而且千万别忘记对lpCmd进行初始化赋值。memset(lpCmd,0,sizeof(TCHAR)*260);

PS C:\Users\34171\Desktop\code\WJ> g++ -o WJ Recipe.c main.c main.c: In function 'void read_string(char*, int)': main.c:15:21: error: 'strchr' was not declared in this scope 15 | char *newline = strchr(buffer, '\n'); | ^~~~~~ main.c:4:1: note: 'strchr' is defined in header '<cstring>'; did you forget to '#include <cstring>'? 3 | #include <locale.h> +++ |+#include <cstring> 4 | main.c: In function 'int main()': main.c:102:21: error: 'strlen' was not declared in this scope 102 | if (strlen(updated.name) == 0) { | ^~~~~~ main.c:102:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:103:21: error: 'strcpy' was not declared in this scope 103 | strcpy(updated.name, existing->name); | ^~~~~~ main.c:103:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:108:21: error: 'strlen' was not declared in this scope 108 | if (strlen(updated.category) == 0) { | ^~~~~~ main.c:108:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:109:21: error: 'strcpy' was not declared in this scope 109 | strcpy(updated.category, existing->category); | ^~~~~~ main.c:109:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:114:21: error: 'strlen' was not declared in this scope 114 | if (strlen(updated.ingredients) == 0) { | ^~~~~~ main.c:114:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:115:21: error: 'strcpy' was not declared in this scope 115 | strcpy(updated.ingredients, existing->ingredients); | ^~~~~~ main.c:115:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:121:21: error: 'strlen' was not declared in this scope 121 | if (strlen(input) > 0) { | ^~~~~~ main.c:121:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:122:41: error: 'atoi' was not declared in this scope 122 | updated.prep_time = atoi(input); | ^~~~ main.c:127:21: error: 'strlen' was not declared in this scope 127 | if (strlen(input) > 0) { | ^~~~~~ main.c:127:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:128:41: error: 'atoi' was not declared in this scope 128 | updated.cook_time = atoi(input); | ^~~~ main.c:133:21: error: 'strlen' was not declared in this scope 133 | if (strlen(updated.steps) == 0) { | ^~~~~~ main.c:133:21: note: 'strlen' is defined in header '<cstring>'; did you forget to '#include <cstring>'? main.c:134:21: error: 'strcpy' was not declared in this scope 134 | strcpy(updated.steps, existing->steps); | ^~~~~~ main.c:134:21: note: 'strcpy' is defined in header '<cstring>'; did you forget to '#include <cstring>'? PS C:\Users\34171\Desktop\code\WJ>
最新发布
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值