Hello All,
if i want to declare a string but i don't know how big it would be, it could be 300 or 7000 or maybe more.
for example:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char *FristFun(char *Parm){
char ReturnStr=malloc(1*sizeof(char));
//Do somthing unique in this function
ReturnStr = realloc(ReturnStr,(strlen(ReturnStr) + strlen(Parm)+1));
strcpy(ReturnStr , Parm);
return ReturnStr;
}
char *SecondFun(char *Parm){
char ReturnStr = malloc(1*sizeof(char));
//Do somthing unique in this function
ReturnStr = realloc(ReturnStr,(strlen(ReturnStr) + strlen(Parm)+1));
strcpy(ReturnStr , Parm);
return ReturnStr;
}
char *ThirdFun(char *Parm){
char ReturnStr = malloc(1*sizeof(char));
//Do somthing unique in this function
ReturnStr = realloc(ReturnStr,(strlen(ReturnStr) + strlen(Parm)+1));
strcpy(ReturnStr , Parm);
return ReturnStr;
}
int main() {
char FristReturn = malloc(1*sizeof(char));
char SecondReturn = malloc(1*sizeof(char));
char ThirdReturn = malloc(1*sizeof(char));
{//Get Values
strcpy(FristReturn,FristFun("111111111111111111111111"));
strcpy(SecondReturn,SecondFun("2222222222222222222222"));
strcpy(ThirdReturn,ThirdFun("3333333333333333333"));
}
printf("\n============================================\n");
printf("FristReturn:\n\t%s\n",FristReturn);
printf("SecondReturn:\n\t%s\n", SecondReturn);
printf("ThirdReturn:\n\t%s\n", ThirdReturn);
printf("\n============================================\n\n");
{//Free Var's
free(FristReturn);
free(SecondReturn);
free(ThirdReturn);
}
return 0;
}
this is just an example the function should return a bigger string than this. however the programing dose not seem to be working and from what i understand i have to use malloc and realloc to change the size of char.
Therefore, how can i declare a dynamic char that could be as big as what i store in it ?
Any help would be much appreciated.
Bookmarks