#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char *GetMemory(int size)//函数指针 *不能少
{
char *a=NULL;
a=(char *)malloc(sizeof(100));
return a;
}
void get(char **str,int size)
{
*str=(char *)malloc(sizeof(100));
}
void fun(char *str)
{
strcpy(str,"1789");
}
char *func(void)
{
char *p;
p=(char *)malloc(sizeof(128));
strcpy(p,"hello world");
return p;
}
int main()
{
char *x=NULL;
x=(char *)malloc(sizeof(128));
x=func();
printf("%s\n",x);
free(x);
x=NULL;
//1.在一个函数中申请和释放
char *p=NULL;
p=(char *)malloc(sizeof(100));
strcpy(p,"helloworld");
printf("%s\n",p);
free(p);
p=NULL;
//2.在一个子函数中分配
char *s=NULL;
s=GetMemory(100);
strcpy(s,"123456");
printf("%s\n",s);
free(s);
s=NULL;
//3.从一个子函数的参数返回内存,不通过return的方式
char *q=NULL;
get(&q,100);
strcpy(q,"qqqq");
printf("%s\n",q);
free(q);
q=NULL;
char *w=NULL;
w=(char *)malloc(sizeof(100));
fun(w);
printf("%s\n",w);
free(w);
w=NULL;
}
#include<stdlib.h>
#include<string.h>
char *GetMemory(int size)//函数指针 *不能少
{
char *a=NULL;
a=(char *)malloc(sizeof(100));
return a;
}
void get(char **str,int size)
{
*str=(char *)malloc(sizeof(100));
}
void fun(char *str)
{
strcpy(str,"1789");
}
char *func(void)
{
char *p;
p=(char *)malloc(sizeof(128));
strcpy(p,"hello world");
return p;
}
int main()
{
char *x=NULL;
x=(char *)malloc(sizeof(128));
x=func();
printf("%s\n",x);
free(x);
x=NULL;
//1.在一个函数中申请和释放
char *p=NULL;
p=(char *)malloc(sizeof(100));
strcpy(p,"helloworld");
printf("%s\n",p);
free(p);
p=NULL;
//2.在一个子函数中分配
char *s=NULL;
s=GetMemory(100);
strcpy(s,"123456");
printf("%s\n",s);
free(s);
s=NULL;
//3.从一个子函数的参数返回内存,不通过return的方式
char *q=NULL;
get(&q,100);
strcpy(q,"qqqq");
printf("%s\n",q);
free(q);
q=NULL;
char *w=NULL;
w=(char *)malloc(sizeof(100));
fun(w);
printf("%s\n",w);
free(w);
w=NULL;
}