[LCP] Newbie Question: Initializing empty/null string in C

Ludwig Isaac Lim ludz_lim at yahoo.com
Mon Jun 11 08:03:51 UTC 2007


Hi:

   I have a newbie question (I wasn't able to find the
answer using google).

   What is the best way to initilize an empty/null string
in C? suppose I have a char* str, how should I initialize
it. 

   char* str;

   1) Approach 1 :
         str = "";

   2) Approach 2 :
        str[0] ='\0';

   3) Approach 3 :
       strcpy(str,"");

   4) Approach 4 :
      *str = '\0';

   I suppose there's no need to allocate a space for the
string terminator character ('\0') using:
   str = (char*) malloc(sizeof(char));
   prior to initialization.


Thanks in advance,
ludwig lim


       
____________________________________________________________________________________
Get the free Yahoo! toolbar and rest assured with the added security of spyware protection.
http://new.toolbar.yahoo.com/toolbar/features/norton/index.php



More information about the linuxCprogramming mailing list