我的源文件有一个函数,我在反序列化通过 C 套接字传递的结构的过程中调用它(这是客户端源):
MY_STRUCT_TYPE myStructVar;
unsigned char * deserialize_chararr(unsigned char *buffer, unsigned char** value)
{
unsigned char val[256];
int i;
for(i = 0; buffer[i]; i++) {
val[i] = buffer[i];
}
val[i++] = '\0';
printf("\n chararr: %s :", val);
*value = malloc(i * sizeof **value);
for(i = 0; buffer[i]; i++) {
(*value)[i] = buffer[i];
printf("%c", (*value)[i]);
}
(*value)[i++] = '\0';
/* buffer contains a serialized struct read over a socket using recv.
so this call returns the buffer pointer that get's passed to the next deserialize_<type> invocation a la https://stackoverflow.com/a/1577174/434145
That's why the first string extraction followed by an attempt to set it in the struct as part of unpacking. */
return buffer + i;
}
我称之为
buffer = deserialize_chararr(buffer, &myStructVar->szrecordid);
在哪里
typedef struct _MY_STRUCT_TYPE {
unsigned char szrecordid[28];
}
服务器可以正常发送数据,它甚至可以在上述函数中正确解析,但我似乎无法在我的结构的 String 变量中设置它。看到这个后,我将函数更改为使用 char ** 参数,但打印时仍然会出现垃圾
printf("\n Payload:" "\n szrecordid: %s ", myStructVar.szrecordid);
简而言之,我将一个指针传递给一个字符串 (&myStructVar->szrecordid),使用 malloc 并按字符分配我的指针字符(我从链接中获得的后一部分)。在我将代码从使用 char * 更改为使用 char ** 之前和之后,我都收到了相同的错误和编译器警告:
client.c:159: warning: passing arg 2 of `deserialize_chararr' from incompatible pointer type
那么我做错了什么?