public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: llewelly@198.dsl.xmission.com
To: Shawn <NRLax27@aol.com>
Cc: help-gcc@gnu.org
Subject: Re: Declaring variables mid-function
Date: Fri, 31 Dec 1999 22:24:00 -0000	[thread overview]
Message-ID: <Pine.LNX.4.10.9912181639200.16024-100000@198.dsl.xmission.com> (raw)
Message-ID: <19991231222400.BnL2Of5kYyqVph-XRIY_kaoI28X4F7EYZb3-QuIJqy8@z> (raw)
In-Reply-To: <385c11fa@oit.umass.edu>

On Sat, 18 Dec 1999, Shawn wrote:

>     I am writing a medium sized program, and ran into a problem where gcc
> would not compile any function that did not have all of its variables
> declared as the first lines of the function.  As a test, I wrote this small
> program:
> 

ANSI C requires that all variables be declared at the begining of the
  block.

> int test(char *tst, char *tst2, char **tst3);
> int test2(void);
> 
> int main()
> {
>  test2();
> 
>  char *tst;
>  char *tst2;
>  char *tst3;
> 
>  test(tst, tst2, &tst3);
> 
>  return 1;
> 
> }
> 
> int test(char *tst, char *tst2, char **tst3)
> {
>  return 21;
> }
> 
> int test2(void)
> {
>  return 21;
> }
> 
> When trying to compile this, seemingly simple program, gcc complains.  This
> is the output:
> 
> bash-2.03$ gcc test.c -o test
> test.c: In function `main':
> test.c:8: parse error before `char'
> test.c:12: `tst' undeclared (first use in this function)
> test.c:12: (Each undeclareed identifier is reported only once
> test.c:12: for each function it appears in.)
> test.c:12: `tst2' undeclared (first use in this function)
> test.c:12: `tst3' undeclared (first use in this function)

This is ANSI conformant behavior; any ANSI C compiler will give you
  similar errors.

> 
> Does anybody have any idea what is going on here?  My system is a Pentium,
> with 48 MB ram, running Slackware Linux 7.0, gcc version 2.91.66 (egcs).
> Thank you in advance!
> 

Perhaps you are too acustomed to C++. Unlike C, C++ *does* allow
  declaration of variables anywhere in a block. If you wish to code in 
  C++, compile your code with 'g++', as in: 'g++ test.c -o test' 

Your example compiles perfectly with g++; it is correct C++, but not
  correct C.


  reply	other threads:[~1999-12-31 22:24 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-12-18 15:01 Shawn
1999-12-18 15:52 ` llewelly [this message]
1999-12-31 22:24   ` llewelly
1999-12-18 15:56 ` Jamie Walker
1999-12-18 16:11   ` bowman
1999-12-18 17:44     ` Brendan Murray
1999-12-31 22:24       ` Brendan Murray
1999-12-21  9:19     ` Arthur Gold
1999-12-31 22:24       ` Arthur Gold
1999-12-31 22:24     ` bowman
1999-12-19  4:01   ` David Wragg
1999-12-19 12:15     ` Jamie Walker
1999-12-31 22:24       ` Jamie Walker
1999-12-31 22:24     ` David Wragg
1999-12-31 22:24   ` Jamie Walker
1999-12-18 16:02 ` Erik de Castro Lopo
1999-12-31 22:24   ` Erik de Castro Lopo
1999-12-31 22:24 ` Shawn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Pine.LNX.4.10.9912181639200.16024-100000@198.dsl.xmission.com \
    --to=llewelly@198.dsl.xmission.com \
    --cc=NRLax27@aol.com \
    --cc=help-gcc@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).