0

Hi,
The following part of code is failling at first free call (Line no.26) giving above error.I don't know what to do.Please reply urgently.

int main()
{
	val1 Valstruct;
	int i;
	Valstruct.ch = (char **)malloc(2 * sizeof(char *));
	for(i=0 ; i<2 ; i++)
	{
	    Valstruct.ch[i] = (char *)malloc(10 * sizeof(char));
		Valstruct.ch[i] = "Anand";
		printf("\n String is %s",Valstruct.ch[i]);
	}

	for(i=0 ; i<2 ; i++)
	{
	    free(Valstruct.ch[i]);
	}

	free(Valstruct.ch);
	
	return 0;
}

Edited by __avd: Added [code] tags.

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by pinkannu
0

>> The following part of code is failling at first free call (Line no.26) giving above error.

Please use CODE tags when you post code.

Valstruct.ch[i] = "Anand";

Change the above line to use strcpy() instead of the faulty string literal assignment. free() is attempting to free memory at the location where the string literal "Anand" is stored -- hence your error.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.