I'm trying to create and use a pointer to a string and join it with another reserved string pointer with malloc
, but I always get this error message:
This is the code:
int wmain(int argc, WCHAR *argv[])
{
LPWSTR str1 = L"Software\WinSide\";
LPWSTR str2 = (LPWSTR)malloc(wcslen(str1)+1);
wcscpy(str2, str1);
if (str2 == NULL)
{
wprintf(L"Malloc error.\n");
}
else
{
wprintf(L"Result= %s\n", str2);
}
free(str2);
return 0;
}
If I put a value in% co_of big% like 50, for example, it works fine. I know the problem is in memory allocation, but I do not understand why.
Could you help me?
Edit:
If I use the standard C data types, the program works, but I do not understand why. I add the code:
int main(int argc, char * argv[])
{
char * str1 = "Software\WinSide\";
char * str2 = (char *)malloc(strlen(str1)+1);
strcpy(str2, str1);
if (str2 == NULL)
{
printf("Malloc error.\n");
}
else
{
printf("Result= %s\n", str2);
}
free(str2);
return 0;
}