954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Size

Are the following array declarations valid, if not why?: "i'm confused!"
const int SIZE=4;
void main()
{
int a[SIZE]= {0,2,4,6};
static int b[SIZE]={0,2,4,6,8};
int x[SIZE-5];
int d[SIZE*2];
. . .
. . .
}

Faramba
Newbie Poster
5 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

>>static int b[SIZE]={0,2,4,6,8};
SIZE = 4; b[SIZE] has 5 elements - not good

>>int x[SIZE-5];
SIZE-5 = -1. You cannot declare an array with negative dimention.

Also, int main () , not void main ().
And, for defining the size of an array, I prefer #define SIZE X instead const int SIZE=X,

frrossk
Posting Whiz in Training
220 posts since Sep 2004
Reputation Points: 17
Solved Threads: 9
 

#define is old C style syntax, in C++ const is preferred.

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 
Are the following array declarations valid, if not why?: "i'm confused!" const int SIZE=4; void main() { int a[SIZE]= {0,2,4,6}; ---------------(1) static int b[SIZE]={0,2,4,6,8}; int x[SIZE-5]; int d[SIZE*2]; . . . . . . }


You can re-write the statement (1) as follows:
int a[] = {0,2,4,6};
void main()
{

...

}

Richard Wong
Newbie Poster
2 posts since Jul 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You