free()用于Structure成员

时间:2014-11-16 13:37:41

标签: c pointers malloc structure

#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct sample
{
   int a;
   char *String;
};

void func1(struct sample *par)
{

   par->String = (char *) malloc( 10 * sizeof(char));
   par->a = 3044;
   par->String = "xyz12345";
}

main()
{
   struct sample *x;
   x = (struct sample *) malloc(sizeof(struct sample));
   func1(x);
   printf("values = %d --- %s\n", x->a,x->String);
   free(x->String);     
   free(x);   
   return 0;
}

这里我想释放分配给x-&gt; String的内存。 free(x-> String)输出分段错误。任何人都可以告诉我如何释放分配给x-&gt; String的内存?

0 个答案:

没有答案