#include<stdio.h>
#include<string.h>
#include <stdlib.h>
#pragma warning(disable : 4996)
//动态内存管理
//定义数组内存固定的,但是有时候数组不能确定个数,所以用动态内存
int main(int argc, const char *argv[])
{
char charArray[100];
char *charP;
strcpy(charArray,"CharArray");
charP = (char*)malloc(500*sizeof(charP));
if (charP == NULL)
{
printf("没有分配到相应内存\n");
}
else {
strcpy(charP, "成功分配了动态内存");
}
printf("charArray = %s\n", charArray);
printf("charP = %s\n", charP);
charP = (char*)realloc(charP,200*sizeof(char));
if (charP == NULL)
{
printf("重新分配内存失败\n");
}
else {
strcat(charP, "成功重新分配内存");
}
printf("charArray = %s\n", charArray);
printf("charP = %s\n", charP);
free(charP); //释放指针内存
return 0;
}