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;
}

Recommended Answers

All 2 Replies

>> 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.

Hi,
Thanks for your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.