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

Arjun satish arjun.satish at gmail.com
Mon Jun 11 11:57:13 UTC 2007


Hi Ludwig,

Don't use str unless you assign memory to it using the malloc call.

If its an array, like

char str[SIZE] = {0};

then you can directly use the strlen call...

Regards,
Arjun

On 6/11/07, Ludwig Isaac Lim <ludz_lim at yahoo.com> wrote:
>
>
> 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
>



-- 
And it's whispered that soon, if we all call the tune,
Then the piper will lead us to reason
And a new day will dawn for those who stand long
And the forest will echo with laughter

http://freeshells.ch/~phoenix
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.linux.org.au/pipermail/linuxcprogramming/attachments/20070611/984013e7/attachment.htm 


More information about the linuxCprogramming mailing list