我自己也很困惑,但这就是我所拥有的.我最近才开始用指针来自己熟悉更多的一个点,我觉得更舒适的使用他们,但我得到关于strcpy_s缓冲器()太小错误.
请使用字符数组代替的std :: string,关于我的任何意见及其对围绕字符数组居中HL2SDK(不知道为什么),所以我只是坚持的模式.
void func_a()
{
char *szUserID = new char[64];
char *szInviterID = new char[64];
char *szGroupID = new char[64];
sprintf(szUserID, "%I64d", GetCommunityID(szUserSteamID));
sprintf(szInviterID, "%I64d", GetCommunityID(g_CvarSteamID.GetString()));
GetGroupCommunityID(1254745, &szGroupID); // Group Steam Community ID
}
void GetGroupCommunityID(int groupID, char **communityID)
{
int staticID = 1035827914;
int newGroupID = 29521408 + groupID;
char *buffer = new char[64];
snprintf(buffer, sizeof(buffer), "%d%d", staticID, newGroupID);
strcpy_s(*communityID, sizeof(*communityID), buffer);
delete buffer;
}
Run Code Online (Sandbox Code Playgroud)
问题是您正在使用sizeof哪个是编译时构造来确定运行时的长度*communityID.这基本上可以解决sizeof(char*).你想要的是可用的字节数/字符数*communityID.此信息需要与值一起传递
GetGroupCommunityID(1254745, &szGroupID, sizeof(szGroupID));
void GetGroupCommunityID(int groupId, char** communityID, size_t length) {
...
strcpy_s(*communityID, length, buffer);
}
Run Code Online (Sandbox Code Playgroud)
同样在这个例子中,双指针是不必要的,因为你没有改变指针,只是它的内容.一个指针就可以了
GetGroupCommunityID(1254745, szGroupID, sizeof(szGroupID));
void GetGroupCommunityID(int groupId, char* communityID, size_t length) {
...
strcpy_s(communityID, length, buffer);
}
Run Code Online (Sandbox Code Playgroud)