Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Howto declare a string without knowing how big it would be in c?

realloc string declare

  • Please log in to reply
3 replies to this topic

#1 solo9300

solo9300

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 25 April 2010 - 08:14 PM

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:
#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.
  • 0

#2 deskchecked

deskchecked

    CC Regular

  • Just Joined
  • PipPipPip
  • 28 posts

Posted 25 April 2010 - 09:28 PM

Not really 100% clear what you're asking here. Are you asking how to dynamically concatenate strings of "unknown" length? If so:

// #include <stdarg.h>

char* strdupcat(const char* first, ...) {
  va_list args;
  char* buf;
  const char* arg;
  size_t len = strlen(first);

  /* 1. calculate the length of the output string */
  va_start(args, first);
  while ((arg = va_arg(args, const char*)) != NULL) {
    len += strlen(arg);
  }
  va_end(args);

  /* 2. allocate memory for the output string */
  buf = malloc(len + 1);
  if (buf == NULL) return NULL;

  /* 3. copy strings into the output buffer */
  strcpy(buf, first);
  va_start(args, first);
  while ((arg = va_arg(args, const char*)) != NULL) {
    strcat(buf, arg);
  }
  va_end(args);
  return buf;
}

Then you can build stuff up like so (as a really bad example):

char* temp1 = strdup("first");
char* temp2 = strdupcat(temp1, " second", NULL);
free(temp1);
temp1 = strdupcat(temp2, " third", NULL);
free(temp2);
// at this point, temp1 contains the string "first second third"

You can also use it to concatenate multiple strings at once:

char* test = strdupcat("first", " ", "second", " ", "third", NULL);
// "first second third" again.

Not sure if this is what you're after, but hopefully it helps. If you can more clearly explain what you're trying to do, we might be able to give you more direct help. Posting broken code and saying "It doesn't work" doesn't help us :)

Edited by deskchecked, 25 April 2010 - 09:31 PM.
Comments for strdupcat

  • 0

#3 Guest

Guest

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 914 posts
  • Programming Language:C

Posted 25 April 2010 - 10:13 PM

I have a tutorial on this.
  • 0
Root Beer == System Administrator's Beer
Download the new operating system programming kit! (some assembly required)

#4 outsid3r

outsid3r

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 494 posts

Posted 26 April 2010 - 03:16 AM

You may need to write your own library with a set of functions that auto manage memory like std::string class in C++. I find that using mallocs and reallocs everywhere it's kinda an ugly solution.
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download