#define CLIENT_ACCESS_TOKEN "2064eb7b-81a0-49d4-a819-c482223e1971" int size = 100000; char* data = new char[size]; //cmd data[0] = ""A""; data[1] = ""A""; char token[64]; strcpy(token, CLIENT_ACCESS_TOKEN); memcpy(data + 2*sizeof(char), &token, 64*sizeof(char)); cout << data; memcpy(data + 66*sizeof(char), &token, 64*sizeof(char)); cout << data; data[129] = ""D""; data[134] = ""\0"";
为什么只有一个2064eb7b-81a0-49d4-a819-c482223e1971, 不应该是两个吗?
应该怎么改?
解决方案:20分
按照你的代码,需要这样改:
cb = strlen(token); memcpy(data + 2*sizeof(char) , token, cb * sizeof(char)); data[cb] = "" ""; // space memcpy(data + (cb + 1) *sizeof(char) , token, cb * sizeof(char)); data[cb + 1 + cb] = "" "";
更清晰一点的代码可以这样写
#define CLIENT_ACCESS_TOKEN "2064eb7b-81a0-49d4-a819-c482223e1971" int size = 100000; char* data = new char[size]; //cmd char * p = data; *p++ = ""A""; *p++ = ""A""; char token[64]; size_t cb; strcpy(token, CLIENT_ACCESS_TOKEN); cb = strlen(token); memcpy(p, token, cb); p += cb; *p++ = "" ""; // space; memcpy(p, token, cb); p += cb; *p++ = "" ""; // space *p++ = ""D""; *p++ = ""\0""; // 输出data printf("%s\n", data);