#include<stdio.h>
#include <stdlib.h>
#include <string.h>
void getmemory(char **p, int num)
{
*p=(char *)malloc(num);
printf("*p=%ld/n",*p);
printf(" p=%ld/n",p);
//return *p;
}
void main()
{
char aa[100];
char *str=NULL;
getmemory(&str,100);
printf(" str=%ld/n",str);
printf("&str=%ld/n",&str);
strcpy(str,"hello/n");
printf(str);//输出字符串"hello"
free(str);//释放内存空间,若没有该行代码,将产生内存泄漏
printf(str);//内存空间被释放后,其内容不可预知
char stre[]="hello";
char *pp;
pp=stre;
printf("%ld/n",sizeof(*pp));
printf("%c/n",*pp);
}
程序运行结果:
*p=3606440
p=1244952
str=3606440
&str=1244952
hello
葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺
葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺葺?
h
Press any key to continue