Thread: How can I destroy a character array?

  1. #1
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    How can I destroy a character array?

    I just want to release or destroy the contents of string[] and reset it back to its original state so my if statements will prove false.


    int main()

    {


    int random_number;
    float count = GetTickCount();
    char string[7];
    bool end = false;



    while(end != true)
    {
    srand(GetTickCount());
    random_number = rand()%20 + 1;

    printf("\nType \"roll\" and hit <ENTER> to roll a");
    printf(" random number, or to quit.\n\n");

    fgets(string, 7, stdin);

    if(strlen(string) > 5)
    {
    _flushall();

    //(release contents of string[])

    printf("true");
    }

    if(strnicmp(string, "exit", 4)==NULL)
    {
    printf("\nExiting...\n\n");
    end = true;
    _flushall();
    exit(0);
    }

    if(strnicmp(string, "roll", 4)==NULL)
    {
    printf("\n%.0f\n\n", count);
    printf("%i\n", random_number);
    }


    }

    return 0;
    }


    -- Well I solved it by using a bool value and a conditional && statement, but I'd still like to know how to destroy a character array. --
    Last edited by Invincible; 02-11-2002 at 02:22 AM.
    "The mind, like a parachute, only functions when open."

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    you could try.....

    string[0] = '\0'; /* note single quotes*/
    or
    strcpy(string, "\0"); /* double quotes */

    these will set first element in string to null character. Equivalent I suppose of string = NULL;
    Last edited by bigtamscot; 02-11-2002 at 03:53 AM.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  3. #3
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Thumbs up

    Thanks. I'm still new, sometimes I get lost in all the functions. Your help is greatly appreciated.
    "The mind, like a parachute, only functions when open."

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    If you want to fully clear the string, try
    memset ( string, '\0', SIZE );

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User Invincible's Avatar
    Join Date
    Feb 2002
    Posts
    210

    Thumbs up

    You rule Prelude. That's exactly what I want to do. Thanks.
    "The mind, like a parachute, only functions when open."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM