int SetUpTCPtoSendInformation(char option[], char *command) { static char *TCPCommand; if (strstr(option, "set")) { TCPCommand = malloc(strlen(command) + 1); if (TCPCommand == NULL) { CON_LOG("Failed to set and obtain TCP command variable memory allocation"); return 0; } strcpy(TCPCommand, command); return 1; } else if (strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand) ) { strcpy(command, TCPCommand); memset(TCPCommand, '\0', strlen(TCPCommand)); isTCPCommandSet = 0; free(TCPCommand); return 1; } strcpy(command, ""); return 0; } char a[32] = "zhangsan"; SetUpTCPtoSendInformation("set",a); SetUpTCPtoSendInformation("get",a); printf("%s",a); SetUpTCPtoSendInformation("get",a); printf("%s",a);
时间: 2024-04-08 13:33:45 浏览: 105
CString-int-string-char.zip_strstr
这段代码修正了之前的错误,并且去掉了`isTCPCommandSet`变量,因为它没有被使用。下面是修正后的代码:
```c
int SetUpTCPtoSendInformation(char option[], char *command) {
static char *TCPCommand;
if (strstr(option, "set")) {
TCPCommand = malloc(strlen(command) + 1);
if (TCPCommand == NULL) {
CON_LOG("Failed to set and obtain TCP command variable memory allocation");
return 0;
}
strcpy(TCPCommand, command);
return 1;
} else if (strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) {
strcpy(command, TCPCommand);
memset(TCPCommand, '\0', strlen(TCPCommand));
free(TCPCommand);
return 1;
}
strcpy(command, "");
return 0;
}
char a[32] = "zhangsan";
SetUpTCPtoSendInformation("set", a);
SetUpTCPtoSendInformation("get", a);
printf("%s", a);
SetUpTCPtoSendInformation("get", a);
printf("%s", a);
```
这段代码的逻辑和之前的一样,首先通过`SetUpTCPtoSendInformation("set", a)`将字符串"zhangsan"设置为TCP命令。然后通过`SetUpTCPtoSendInformation("get", a)`获取TCP命令,并打印出来。最后再次调用`SetUpTCPtoSendInformation("get", a)`获取TCP命令,打印结果为空字符串。
阅读全文