public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* statements following strcpy cause parse error
@ 2002-02-07 15:41 Grant Gossett
  2002-02-07 16:01 ` bjorn rohde jensen
  0 siblings, 1 reply; 3+ messages in thread
From: Grant Gossett @ 2002-02-07 15:41 UTC (permalink / raw)
  To: gcc-help

Can anyone tell me if I am overlooking something here?


I have 2 programs here, one which compiles in gcc 2.96 and 3.03 and the
other which gives a parse error "before the last declaration of a char
array".

prog #1 which compiles fine:

#include <string.h>
int main()
{
     char filename[256];
     strcpy (filename, "/etc/bogus.conf\0");
     return 0;
}


prog # 2 which throws a parse error before 'char'  (meaning the strcpy
line??)

#include <string.h>
int main()
{
     char filename[256];
     strcpy (filename, "/etc/bogus.conf\0");
     char unused[256];
     return 0;
}

Am I missing something very obvious here? I've tried the above 2 programs
with 2 different distributiuons of linux (suse 7.2, redhat 7.2), 2
different versions of gcc (2.96 and 3.03) and the verision of glibc shipped
with RH 7.2 as well as the newest glibc available for download. I should
also mention I tried this with Visual C++ 6.0 and also the second wouldn't
compile but it gives a " missing ; before 'type' "error. This most likely
isn't just related to gcc, but I can't for the life of me figure out what
stupidity I am perpetrating here. I also should note that a declaration
such as "int bob = 0" instead of char unused also causes the same issue.

TIA
Grant


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

* Re: statements following strcpy cause parse error
  2002-02-07 15:41 statements following strcpy cause parse error Grant Gossett
@ 2002-02-07 16:01 ` bjorn rohde jensen
  0 siblings, 0 replies; 3+ messages in thread
From: bjorn rohde jensen @ 2002-02-07 16:01 UTC (permalink / raw)
  Cc: gcc-help

Hi Grant,

 In prog#2 you declare a variable in the function body, that is a C++
feature, so prog#2 is legal C++ but illegal C. You have to move the
declaration to the top of main to make it legal C.

Yours sincerely,

Bjorn

Grant Gossett wrote:
> 
> Can anyone tell me if I am overlooking something here?
> 
> I have 2 programs here, one which compiles in gcc 2.96 and 3.03 and the
> other which gives a parse error "before the last declaration of a char
> array".
> 
> prog #1 which compiles fine:
> 
> #include <string.h>
> int main()
> {
>      char filename[256];
>      strcpy (filename, "/etc/bogus.conf\0");
>      return 0;
> }
> 
> prog # 2 which throws a parse error before 'char'  (meaning the strcpy
> line??)
> 
> #include <string.h>
> int main()
> {
>      char filename[256];
>      strcpy (filename, "/etc/bogus.conf\0");
>      char unused[256];
>      return 0;
> }
> 
> Am I missing something very obvious here? I've tried the above 2 programs
> with 2 different distributiuons of linux (suse 7.2, redhat 7.2), 2
> different versions of gcc (2.96 and 3.03) and the verision of glibc shipped
> with RH 7.2 as well as the newest glibc available for download. I should
> also mention I tried this with Visual C++ 6.0 and also the second wouldn't
> compile but it gives a " missing ; before 'type' "error. This most likely
> isn't just related to gcc, but I can't for the life of me figure out what
> stupidity I am perpetrating here. I also should note that a declaration
> such as "int bob = 0" instead of char unused also causes the same issue.
> 
> TIA
> Grant

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

* RE: statements following strcpy cause parse error
       [not found] <616BE6A276E3714788D2AC35C40CD18D3F27F5@whale.softwire.co.uk>
@ 2002-02-08  1:28 ` Rupert Wood
  0 siblings, 0 replies; 3+ messages in thread
From: Rupert Wood @ 2002-02-08  1:28 UTC (permalink / raw)
  To: 'Grant Gossett'; +Cc: gcc-help, shamus

Bjorn Rohde Jensen wrote:

> > prog # 2 which throws a parse error before 'char'  (meaning 
> > the strcpy
> > line??)
> > 
> > #include <string.h>
> > int main()
> > {
> >      char filename[256];
> >      strcpy (filename, "/etc/bogus.conf\0");
> >      char unused[256];
> >      return 0;
> > }
>
>  In prog#2 you declare a variable in the function body, that is a C++
> feature, so prog#2 is legal C++ but illegal C. You have to move the
> declaration to the top of main to make it legal C.

Yes; alternately, you can introduce a new scope for the new variable:

    #include <string.h>
    int main()
    {
         char filename[256];
         strcpy (filename, "/etc/bogus.conf\0");

         {
             char unused[256];
         }

         return 0;
    }

because you are also allowed to introduce new variables at the top of
new scopes (which applies equally to if/for/while scopes, etc.). I've
heard apocryphal stories about compilers mis-optimising this
construction - but you're safe with GCC :-)

You also don't need the \0 at the end of the string unless you
deliberaltely want two - you automatically get nuls at the end of string
constants. (And as a strcpy source, you *don't* need two.) Further style
- you may want to use strncpy instead:

         char filename[256];
         strncpy (filename, "/etc/bogus.conf\0", 256);

or maybe

         strncpy (filename, "/etc/bogus.conf\0", sizeof(filename));

just to get into the habit of using it if nothing else; this helps
prevent buffer-overflow security holes.

(You may even want avoid assuming that characters are size 1

         strncpy (filename, "/etc/bogus.conf\0",
                  sizeof(filename)/sizeof(filename[0]));

for unicode, etc. - but then you'd need to change function and
string-constant style, etc.)

Rup.

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

end of thread, other threads:[~2002-02-08  9:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-02-07 15:41 statements following strcpy cause parse error Grant Gossett
2002-02-07 16:01 ` bjorn rohde jensen
     [not found] <616BE6A276E3714788D2AC35C40CD18D3F27F5@whale.softwire.co.uk>
2002-02-08  1:28 ` Rupert Wood

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).