public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Is this code valid under g++
@ 2003-03-20 18:39 Ajay Bansal
  2003-03-20 19:07 ` LLeweLLyn Reese
  0 siblings, 1 reply; 4+ messages in thread
From: Ajay Bansal @ 2003-03-20 18:39 UTC (permalink / raw)
  To: gcc-help

Is the following code corect??


char*
getNextItem(char* line, const char* delimit)
{
    // Retrieve the next item, and strip off any whitespace around it
    char* item;
    char* tmp;

    item = strtok(line, delimit);
    if (item)
    {
        // Strip off leading whitespace
        while (*item == ' ' || *item == '   ')
        {
            item++;
        } // while

        // Find any trailing spaces, tabs, newlines
        tmp = strpbrk(item, " \t\n");
        if (tmp)
        {

            *tmp = '\0';
        } // if

        // Comment, or end of line
        if (*item == '#' || *item == '\n' || *item == '\0')
        {
            // Return as if nothing
            item = 0;
        } // if
    } // if

    return item;
} // getNextItem()


CAN WE RETURN item (it is local to the function)

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is this code valid under g++
  2003-03-20 18:39 Is this code valid under g++ Ajay Bansal
@ 2003-03-20 19:07 ` LLeweLLyn Reese
  2003-03-20 21:55   ` Oscar Fuentes
  0 siblings, 1 reply; 4+ messages in thread
From: LLeweLLyn Reese @ 2003-03-20 19:07 UTC (permalink / raw)
  To: Ajay Bansal; +Cc: gcc-help

"Ajay Bansal" <Ajay_Bansal@infosys.com> writes:

> Is the following code corect??
> 
> 
> char*
> getNextItem(char* line, const char* delimit)
> {
>     // Retrieve the next item, and strip off any whitespace around it
>     char* item;
>     char* tmp;
> 
>     item = strtok(line, delimit);
>     if (item)
>     {
>         // Strip off leading whitespace
>         while (*item == ' ' || *item == '   ')

No. '   ' is a multicharacter char literal. It is not part of standard
    C++, not portable in general, and AFAIK a gcc extension. If you
    mean a tab, use \t.

>         {
>             item++;
>         } // while
> 
>         // Find any trailing spaces, tabs, newlines
>         tmp = strpbrk(item, " \t\n");
>         if (tmp)
>         {
> 
>             *tmp = '\0';
>         } // if
> 
>         // Comment, or end of line
>         if (*item == '#' || *item == '\n' || *item == '\0')
>         {
>             // Return as if nothing
>             item = 0;
>         } // if
>     } // if
> 
>     return item;
> } // getNextItem()
> 
> 
> CAN WE RETURN item

Yes.

> (it is local to the function)

No.

*item is not local to the function. Upon return, item is either null,
    or points into the char array pointed to by line.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is this code valid under g++
  2003-03-20 19:07 ` LLeweLLyn Reese
@ 2003-03-20 21:55   ` Oscar Fuentes
  2003-03-21  1:23     ` LLeweLLyn Reese
  0 siblings, 1 reply; 4+ messages in thread
From: Oscar Fuentes @ 2003-03-20 21:55 UTC (permalink / raw)
  To: gcc-help

LLeweLLyn Reese <llewelly@lifesupport.shutdown.com> writes:

>>         while (*item == ' ' || *item == '   ')
>
> No. '   ' is a multicharacter char literal. It is not part of standard
>     C++,

That was what I thought, but it's ok, although its value is
implementation-defined. See 2.13.2/1

> not portable in general, and AFAIK a gcc extension. If you
>     mean a tab, use \t.

[snip]

-- 
Oscar

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Is this code valid under g++
  2003-03-20 21:55   ` Oscar Fuentes
@ 2003-03-21  1:23     ` LLeweLLyn Reese
  0 siblings, 0 replies; 4+ messages in thread
From: LLeweLLyn Reese @ 2003-03-21  1:23 UTC (permalink / raw)
  To: Oscar Fuentes; +Cc: gcc-help

Oscar Fuentes <ofv@wanadoo.es> writes:

> LLeweLLyn Reese <llewelly@lifesupport.shutdown.com> writes:
> 
> >>         while (*item == ' ' || *item == '   ')
> >
> > No. '   ' is a multicharacter char literal. It is not part of standard
> >     C++,
> 
> That was what I thought, but it's ok, although its value is
> implementation-defined. See 2.13.2/1

Thank you. I had thought it was undefined, but now I see it isn't.
Next question - where are the gcc docs for multicharacter char literals?

> 
> > not portable in general, and AFAIK a gcc extension. If you
> >     mean a tab, use \t.
> 
> [snip]
> 
> -- 
> Oscar

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-03-21  1:05 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-20 18:39 Is this code valid under g++ Ajay Bansal
2003-03-20 19:07 ` LLeweLLyn Reese
2003-03-20 21:55   ` Oscar Fuentes
2003-03-21  1:23     ` LLeweLLyn Reese

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).