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

Anand, Jessu Jessu.Anand at lsi.com
Mon Jun 11 12:17:37 UTC 2007


Hi Ludwig,
Str = NULL is just initializing it with Null pointer, not filling the
string with zero characters.
After str = NULL if you use strlen, the compiler tries to deference the
pointer which is a Null, there by generating segmentation fault.

For having a zero output, str = "" would hold good.

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 3:50 PM
To: Linux C Programming
Subject: Re: [LCP] Newbie Question: Initializing empty/null string in C


Hi Jessu:

--- "Anand, Jessu" <Jessu.Anand at lsi.com> wrote:

> Hi Ludwig,
> You can initialize it by 
> Str = NULL;
> 
> And later filling the characters, append '\0' to the end
> of it.
> 
> Regards,
> Jessu.

   Thanks for the quick reply. Unfortunately it doesn't
work.

   For example the following program will produce a
segmentation fault:

    int main()
    {
       char *c;
       c = NULL;
       printf("%d",strlen(c));

    } 

   I expect the output to be zero, but it produces a
segmentation fault everytime I ran it.

regards,
ludwig


 
________________________________________________________________________
____________
Park yourself in front of a world of choices in alternative vehicles.
Visit the Yahoo! Auto Green Center.
http://autos.yahoo.com/green_center/ 

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



More information about the linuxCprogramming mailing list