delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/01/25/01:27:08

Date: Thu, 25 Jan 1996 08:16:34 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Mason Glaves <masong AT post DOT cis DOT smu DOT edu>
Cc: djgpp AT delorie DOT com
Subject: Re: MS DOS help needed

On Wed, 24 Jan 1996, Mason Glaves wrote:

> Ok, if I try to add the lines:
> 
> #define MAX 30
> 
> ... (other stuff)
> 
> main()
> 
> char buff[MAX];
> 
> It compiles fineuntil it reaches the above line, but then it give me some 
> error message that looks like:
> 
> in function main
> line 20 : buff undeclared (use this function first).

The compiler is right.  A variable (arrays included) can be defined either
inside a function (which makes it known only to that function) or outside
it (which makes it known to all the functions from that file or even to
the entire program).  In the first case, you should write thusly: 

#define MAX  30

main()
{
  char buff[MAX];
.
. (code for `main')
.
}

In the second case, you should write this:

#define MAX 30

char buff[MAX];

main()
{
.
. (code for `main')
.
}

Also, you might benefit from the -Wall compiler switch that will catch 
much more potential errors in your code and clarify them quite a bit.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019