#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>/* TO DO
1. struct with malloc
2. use calloc:
a.with initailized value 0
b.allocate memory
*/typedefstructcharacter{char* name;
bool rareClothes;int* level;// many levels in physical and mind for using calloc set to zero at the beginning}cha;
cha*createCharacter(char* name, bool rare);voidfreeCharacter(cha* character);intmain(main){
cha* magician =createCharacter("Lily", true);printf("magician: %s, %s, %d\n", magician->name, magician->rareClothes?"true":"false",*(magician->level));freeCharacter(magician);return0;}
cha*createCharacter(char* name,bool rare){
cha* myCharacter =(cha*)malloc(sizeof(cha));if(myCharacter ==NULL){perror("falied to allocate the struct!");returnNULL;}
myCharacter->name =(char*)malloc((strlen(name)+1)*sizeof(char));if(myCharacter->name ==NULL){perror("falied to allocate the string");free(myCharacter);returnNULL;}strcpy_s(myCharacter->name,strlen(name)+1, name);
myCharacter->level =(int*)calloc(1,sizeof(int));if(myCharacter->level ==NULL){perror("falied to calloc the level");free(myCharacter->name);free(myCharacter);returnNULL;}
myCharacter->rareClothes = rare;return myCharacter;}voidfreeCharacter(cha* character){if(character !=NULL){free(character->name);free(character->level);free(character);}}
output
magician: Lily, true,0
F:\Project\C\Project10\x64\Debug\Project10.exe(process 1972) exited with code 0.
Press any key to close this window ...
2.具体值
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<stdbool.h>typedefstructcharacter{char* name;
bool rareClothes;int* level; many levels in physical and mind for using calloc set to zero at the beginning}cha;
cha*createCharacter(char* name, bool rare,int* level);voidfreeCharacter(cha* character);intmain(main){//a number in littleBox,and the littleBox in myCharacter(magicain).//pass the address of littleBox to myCharacter(magicain)int littleBox =7;
cha* magician =createCharacter("Lily", true,&littleBox);printf("magician: %s, %s, %d\n", magician->name, magician->rareClothes?"true":"false",*(magician->level));freeCharacter(magician);return0;}
cha*createCharacter(char* name,bool rare,int* level){
cha* myCharacter =(cha*)malloc(sizeof(cha));if(myCharacter ==NULL){perror("falied to allocate the struct!");returnNULL;}
myCharacter->name =(char*)malloc((strlen(name)+1)*sizeof(char));if(myCharacter->name ==NULL){perror("falied to allocate the string");free(myCharacter);returnNULL;}strcpy_s(myCharacter->name,strlen(name)+1, name);
myCharacter->level =(int*)calloc(1,sizeof(int));if(myCharacter->level ==NULL){perror("falied to calloc the level");free(myCharacter->name);free(myCharacter);returnNULL;}*(myCharacter->level)=*level;
myCharacter->rareClothes = rare;return myCharacter;}voidfreeCharacter(cha* character){if(character !=NULL){free(character->name);free(character->level);free(character);}}
output
magician: Lily, true,7
F:\Project\C\Project10\x64\Debug\Project10.exe(process 7092) exited with code 0.
Press any key to close this window ...