Page 1 of 2 12 LastLast
Results 1 to 10 of 16

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

  1. #1
    Join Date
    May 2009
    Beans
    58

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

    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.

  2. #2
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

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

    no you can't.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #3
    Join Date
    Feb 2007
    Location
    Tuxland
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

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

    You have to keep track of the capacity of the array, and then realloc if the actual size of string gets to the capacity of the array. AFIAK, realloc is not always guaranteed to succeed. What you have to in the case realloc fails is memcpy the entire array to a new, larger array, and then free the old array (although I think realloc will do this for you). Essentially this is what the string (and vector) implementation in C++ does. So you can do it, but it is a very expensive operation to resize a large string, and should be avoided if possible.
    Last edited by phrostbyte; April 26th, 2010 at 04:54 AM.
    Proud GNU/Linux zealot and lover of penguins
    "Value your freedom or you will lose it, teaches history." --Richard Stallman

  4. #4
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

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

    hmm do this?

    Code:
    char* str = "Hello\0";
    Last edited by LKjell; April 26th, 2010 at 06:50 AM. Reason: null terminate

  5. #5
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

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

    Quote Originally Posted by LKjell View Post
    hmm do this?

    Code:
    char* str = "Hello";
    No, the point is that you don't know in advance how large the string will be. Obviously that means you also don't know what it is.

  6. #6
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

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

    Quote Originally Posted by Bachstelze View Post
    No, the point is that you don't know in advance how large the string will be. Obviously that means you also don't know what it is.
    oh trust me you always know. If there is input then a modified fgets and getline will do its job. There is also dynamic allocation during runtime.

  7. #7
    Join Date
    Mar 2006
    Location
    Eefde, The Netherlands
    Beans
    432
    Distro
    Ubuntu 10.04 Lucid Lynx

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

    If you just want to grab a copy of a string then you can do something like this:
    Code:
    char *dupstr(const char *s)
    {
        size_t n = strlen(s) + 1;
        char *p = malloc(n);
        if (p != NULL) memcpy(p, s, n);
        return p;
    }
    Many C implementations provide a function strdup() as an extension, which does what the above function does. strdup() is not part of the standard C library so you would loose a little portability by using it.

    As a side note: sizeof(char) is 1 by definition, so in stead of writing:
    Code:
    x = malloc(n * sizeof(char));
    you can simply write:
    Code:
    x = malloc(n);
    HTH

  8. #8
    Join Date
    Nov 2005
    Location
    Sendai, Japan
    Beans
    11,296
    Distro
    Kubuntu

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

    Quote Originally Posted by Compyx View Post
    As a side note: sizeof(char) is 1 by definition, so in stead of writing:
    Code:
    x = malloc(n * sizeof(char));
    you can simply write:
    Code:
    x = malloc(n);
    However what it one wants to switch from plan chars to, say, Unicode characters? A simple

    Code:
    sed 's/sizeof(char)/sizeof(your_unicode_char_here)/g'
    would do. If you just assumed it to be 1, you're screwed.

  9. #9
    Join Date
    Feb 2007
    Location
    Tuxland
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

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

    Quote Originally Posted by Compyx View Post
    If you just want to grab a copy of a string then you can do something like this:
    Code:
    char *dupstr(const char *s)
    {
        size_t n = strlen(s) + 1;
        char *p = malloc(n);
        if (p != NULL) memcpy(p, s, n);
        return p;
    }
    Many C implementations provide a function strdup() as an extension, which does what the above function does. strdup() is not part of the standard C library so you would loose a little portability by using it.

    As a side note: sizeof(char) is 1 by definition, so in stead of writing:
    Code:
    x = malloc(n * sizeof(char));
    you can simply write:
    Code:
    x = malloc(n);
    HTH
    That's a very nice example. Here is just a possible modification.

    This function _should_ (cross-fingers), resize a string, without modifying its contents. It does so by allocating a new chunk of memory, copying it over, freeing the original chuck, and rebasing the pointer. It assumes s points to some data allocated on the heap, and that n is NOT less then the data allocated. And yes if there are any other pointers to this string, they are invalid after calling this. Might be a way to avoid these disadvantages (double pointers, heh)..

    Code:
    char *strresize(char* const s, size_t n)
    {
        char *p = malloc(++n);
        if (p != NULL) memcpy(p, s, n);
        else return;
        free(s);
        return p;
    }
    Now someone else can code a strcat that calls this function as needed. Wow, a mildly safe strcat in C.
    Last edited by phrostbyte; April 26th, 2010 at 08:24 AM.
    Proud GNU/Linux zealot and lover of penguins
    "Value your freedom or you will lose it, teaches history." --Richard Stallman

  10. #10
    Join Date
    Jan 2007
    Beans
    Hidden!
    Distro
    Ubuntu

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

    modify const is kinda risky... And if you allocate the string on the stack free will give you a nice Christmas tree.
    Last edited by LKjell; April 26th, 2010 at 07:55 AM.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •