static char *TCPCommand; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; int SetUpTCPtoSendInformation(char option[], char **command) { static int isFirstCall = 1; pthread_mutex_lock(&mutex); // 加锁 if (isFirstCall && strstr(option, "set")) { if (TCPCommand != NULL) { free(TCPCommand); } TCPCommand = malloc(strlen(*command) + 1); if (TCPCommand == NULL) { printf("Failed to set and obtain TCP command variable memory allocation\n"); goto fail; } strcpy(TCPCommand, *command); printf("set:%s\n", TCPCommand); isFirstCall = 0; goto succeed; } else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) { free(*command); *command = malloc(strlen(TCPCommand) + 1); strcpy(*command, TCPCommand); printf("get:%s\n", *command); memset(TCPCommand, '\0', strlen(TCPCommand)); free(TCPCommand); TCPCommand = NULL; isFirstCall = 1; goto succeed; } memset(*command, 0, sizeof(*command)); strcpy(*command, ""); fail: pthread_mutex_unlock(&mutex); // 解锁 return 0; succeed: pthread_mutex_unlock(&mutex); // 解锁 return 1; } (cmdBuf)=calloc(BUFFER_SIZE, sizeof(char)); if (ReceivePackets.cmdBuf == NULL) return 0; SetUpTCPtoSendInformation("set","sssssss"); char *resultbuf =NULL; SetUpTCPtoSendInformation("get",&resultbuf); if(strlen(resultbuf)){ free(cmdBuf); cmdBuf=malloc(strlen(resultbuf) + 1); strcpy(cmdBuf, resultbuf); } printf("%d %d %s %s", strlen(cmdBuf), strlen(resultbuf), cmdBuf, resultbuf); 这样子调用对么如果不对请帮我修改函数或者调用方法(SetUpTCPtoSendInformation("set","sssssss");这个不能修改),printf结果为多少
时间: 2024-04-10 21:33:45 浏览: 68
pthread_cond_wait() 用法深入分析
根据您提供的代码,调用`SetUpTCPtoSendInformation`函数的方式是正确的。根据代码逻辑,当调用`SetUpTCPtoSendInformation("set","sssssss")`时,会将`"sssssss"`赋值给`TCPCommand`变量,并打印出`set:sssssss`。
接着,当调用`SetUpTCPtoSendInformation("get",&resultbuf)`时,会将`TCPCommand`的值赋给`resultbuf`,并打印出`get:sssssss`。然后,会将`resultbuf`的值赋给`cmdBuf`,并打印出`strlen(cmdBuf)`、`strlen(resultbuf)`、`cmdBuf`和`resultbuf`的值。
因此,根据您提供的代码和调用方式,最终的printf结果应该是:
```
7 7 sssssss sssssss
```
阅读全文