From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 11496 invoked by alias); 8 Feb 2002 09:28:24 -0000 Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-help-owner@gcc.gnu.org Received: (qmail 11436 invoked from network); 8 Feb 2002 09:28:16 -0000 Received: from unknown (HELO anchor-post-30.mail.demon.net) (194.217.242.88) by sources.redhat.com with SMTP; 8 Feb 2002 09:28:16 -0000 Received: from mailgate.softwire.co.uk ([62.49.203.138] helo=polarbear) by anchor-post-30.mail.demon.net with esmtp (Exim 3.34 #1) id 16Z7KJ-000LBx-0U; Fri, 08 Feb 2002 09:28:15 +0000 From: "Rupert Wood" To: "'Grant Gossett'" Cc: , Subject: RE: statements following strcpy cause parse error Date: Fri, 08 Feb 2002 01:28:00 -0000 Message-ID: <616BE6A276E3714788D2AC35C40CD18D03AA2A@whale.softwire.co.uk> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook, Build 10.0.2627 In-Reply-To: <616BE6A276E3714788D2AC35C40CD18D3F27F5@whale.softwire.co.uk> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 X-SW-Source: 2002-02/txt/msg00085.txt.bz2 Bjorn Rohde Jensen wrote: > > prog # 2 which throws a parse error before 'char' (meaning > > the strcpy > > line??) > > > > #include > > 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 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.