Compiler gives error of missing ']' while it's already there.

  • Follow


Hi! Anybody there who can answer me on my following code:

When I try to compile this code, I get message of a missing ']' on the
line 3 i.e. int a[MAX];, why is this happening

Thanks in anticipation.


VASUDEV MUKHERJEE


#include <stdio.h>
#define MAX 9;
int a[MAX];
int rands=9;
int rand()
{
rands=rands*1103515245 +12345;
return (unsigned int) (rands/65536)%32768;
}
main ()
{
int i,t,x,y;
for(i=0; i<MAX;i++)
{
a[i]=rand();
printf("%d\n", a[i]);
}
}

0
Reply vasudevmukherjee (24) 10/25/2005 12:11:29 PM

In article <1130242289.308797.5710@f14g2000cwb.googlegroups.com>,
 <vasudevmukherjee@yahoo.co.uk> wrote:

>#define MAX 9;
>int a[MAX];

This expands to int a[9;];

-- Richard
0
Reply richard91 (3683) 10/25/2005 12:16:06 PM


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

vasudevmukherjee@yahoo.co.uk wrote:
> Hi! Anybody there who can answer me on my following code:
> 
> When I try to compile this code, I get message of a missing ']' on the
> line 3 i.e. int a[MAX];, why is this happening
> 
> Thanks in anticipation.
> 
> 
> VASUDEV MUKHERJEE
> 
> 
> #include <stdio.h>
> #define MAX 9;

Think of the implication of the above macro when it is used in the line
below

> int a[MAX];

Because of your #define for MAX, the above line will expand to
  int a[9;];
before being parsed

Is this what you intend? Is this legal C? (I know the answers - this is
a hint to you that you should look into the questions a bit)

[snip]

- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDXiKWagVFX4UWr64RAlvbAKDVhYzgX4bEujnIb7R4FY4tnGmxtQCfalcR
VdLxzmKhtCWh405pQStf18s=
=8DSt
-----END PGP SIGNATURE-----
0
Reply Lew.Pitcher (530) 10/25/2005 12:18:31 PM

vasudevmukherjee@yahoo.co.uk wrote:
> Hi! Anybody there who can answer me on my following code:
> 
> When I try to compile this code, I get message of a missing ']' on the
> line 3 i.e. int a[MAX];, why is this happening

> #include <stdio.h>
> #define MAX 9;
> int a[MAX];

This expands to
   int a[ 9; ];
the error of which is clearer, I think, when written
   int a[9;
   ];
Fix it by losing the semicolon from the #define.
0
Reply mambuhl (2201) 10/25/2005 5:01:44 PM

3 Replies
26 Views

(page loaded in 0.09 seconds)


Reply: