The following code throws an access violation at line L2 at runtime, which happens during the 2nd call to setword.
Q> Where am I going wrong in L2, and why is there no problem with the 1st memset at line L1?
Note: I have tried to isolate the problem area from a larger code, hope this provides enough info.
void setword( char ** word )
{
if ( *word == NULL )
{
*word = (char *)malloc(30);
memset( *word, '\0', 30 ); //L1: OK
}
else
{
memset( *word, '\0', 30 );//L2: Access violation
}
*word = "Hello";
//*word shall be freed when operations are complete.
}
int main()
{
char * word = NULL;
setword( &word ); //Call 1: OK
printf( "%s\n", word );
setword( &word ); //Call 2: NOK!
printf( "%s\n", word );
}