This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Is this code valid under g++


"Ajay Bansal" <Ajay_Bansal at infosys dot 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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]