make a string empty in C

This is a discussion on make a string empty in C within the C Programming forums, part of the General Programming Boards category; I need to keep this string empty every time i execute the loop. I done this to do so. Code: ...

  1. #1
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56

    make a string empty in C

    I need to keep this string empty every time i execute the loop.

    I done this to do so.
    Code:
        
    while(some_cond)
        {
        string=(char *)malloc(20);
    while(cond)
    {
        string[index]=assigne_something;
    ++index;
    }
        }
    but I think it is a bad idea. Is there any better idea to do it
    Last edited by sagar474; 09-14-2011 at 09:44 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could use calloc, but all that does is loop through it for you. Either way you are still doing the same thing. But your malloc should be outside of that loop:
    Code:
    char *s = NULL;
    s = malloc( size );
    if( s )
    {
        int x;
        for( x = 0; x < size; x++ )
            s[ x ] = '\0';
    }
    That and calloc effectively do the same thing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The easy way to make a string empty is this...
    Code:
    char string[100000];
    
    string[0] = 0;  // good as empty

  4. #4
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by quzah View Post
    You could use calloc, but all that does is loop through it for you. Either way you are still doing the same thing. But your malloc should be outside of that loop:
    Code:
    char *s = NULL;
    s = malloc( size );
    if( s )
    {
        int x;
        for( x = 0; x < size; x++ )
            s[ x ] = '\0';
    }
    That and calloc effectively do the same thing.


    Quzah.

    no every time i execute the string should have empty I up edited the code.
    sorry it's my fault.
    Last edited by sagar474; 09-14-2011 at 09:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to make an empty square using only 2 for loops
    By Adelbert09 in forum C Programming
    Replies: 2
    Last Post: 07-06-2010, 02:01 PM
  2. String.empty v.s. string.null
    By George2 in forum C# Programming
    Replies: 4
    Last Post: 06-19-2008, 08:22 AM
  3. Flush / Empty a string
    By Buzzer in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2006, 01:36 PM
  4. Tell if a string is empty?
    By dizz in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2002, 12:37 AM
  5. How to empty a string?
    By Zwen in forum C++ Programming
    Replies: 6
    Last Post: 09-30-2002, 07:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21