Setting an empty string

This is a discussion on Setting an empty string within the C++ Programming forums, part of the General Programming Boards category; How do you set an empty string... char string[] = ""; or char string[50] = ""; or are these both ...

  1. #1
    Unregistered
    Guest

    Setting an empty string

    How do you set an empty string...

    char string[] = "";
    or
    char string[50] = "";
    or are these both wrong?

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Nothing can be empty it will have some value in it but if I get what you mean
    Code:
    char string[50] = '\0';
    would set all elements to \0, which is as close to empty as you can get.
    Last edited by C_Coder; 03-03-2002 at 03:50 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Fingerstyle Guitarist taylorguitarman's Avatar
    Join Date
    Aug 2001
    Posts
    564
    They will both work, but perform different purposes.

    char CharArray[] = "Something"; // initialize the character string to what's between the "s plus the \0 character at the end.

    so
    char string[] = ""; // creates an array of one character which would be the \0 character.

    char string[50] = ""; // creates an array of 50 characters with the initial string of \0.

    You are right either way but it depends on what you want to do as to which one you should use.

    A truly "empty" string would be
    char *pString = NULL;
    If a tree falls in the forest, and no one is around to see it, do the other trees make fun of it?

  4. #4
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    cool
    Paro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  2. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 12:40 PM

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