I am trying to write to a C string character-by-character but Visual Studio indicates that the pointer in question is NULL. Even though there are two functions that attempt this, only in the first one does this issue occur.
These are the two functions:
char* revstr(char* string)
{
int len = strlen(string);
char* retstr = (char*)malloc(30);
// printf("%d\n", sizeof(retstr));
int i=0, j=0;
for (i = len, j = 0; i >=0; i--, j++)
{
retstr[j]=string[i]; // this generates C6011: dereferencing NULL pointer 'retstr'
}
printf("\n");
retstr[j] = '\0'; // also generates C6011
printf("%s",etstr);
printf("\n");
return retstr;
}
char * get_name(char* string)
{
char* retstr = (char*)malloc(30);
int i = 0,j=0;
for (i = strlen(string) - 1; i > 0; i--)
if (string[i] == '.')
break;
while (string[i] != '/')
{
// printf("%c", string[i]);
retstr[j] = string[i];
i--; j++;
}
retstr[j] = '\0';
return retstr;
}