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

Anand, Jessu Jessu.Anand at lsi.com
Mon Jun 11 09:27:43 UTC 2007


Hi Ludwig,
You can initialize it by 
Str = NULL;

And later filling the characters, append '\0' to the end of it.

Regards,
Jessu.

-----Original Message-----
From: linuxcprogramming-bounces at lists.linux.org.au
[mailto:linuxcprogramming-bounces at lists.linux.org.au] On Behalf Of
Ludwig Isaac Lim
Sent: Monday, June 11, 2007 1:34 PM
To: linuxCprogramming at lists.linux.org.au
Subject: [LCP] Newbie Question: Initializing empty/null string in C

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

_______________________________________________
This is the Linux C Programming List
:  http://lists.linux.org.au/listinfo/linuxcprogramming List



More information about the linuxCprogramming mailing list