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

V

vasudevmukherjee

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=rand();
printf("%d\n", a);
}
}
 
L

Lew Pitcher

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

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-----
 
M

Martin Ambuhl

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top