Question About Array Initliazation

I'm compiling C code under Lucid Ubunutu using GCC version 4.4.3. I understand that a constant value must be used inside the brackets, and I thought that macros were considered constant since they are replaced by the precompiler. I am curious as to why the following doesn't seem to work under the aforementioned compiler.

#define ARRAY_SIZE 3
int arr[ARRAY_SIZE] = { 1, 2, 3 };

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

phoffric\Commented:
This should work. What problem are you getting?
BTW, you could also write:
     int arr[] = { 1, 2, 3 };
#define ARRAY_SIZE 3
int main() {
   int arr[ARRAY_SIZE] = { 1, 2, 3 };
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Author Commented:
Hmm...  I can't seem to reproduce it now (I've been making changes since I originally encountered the problem). If I recall correctly, it was complaining about too many initializers, but I only had the three, that's why I was confused.
phoffric\Commented:
Well, if I make the macro 2 with three initializers, I got this: warning: excess elements in array initializerDo you think you may have had a mismatch earlier?
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

phoffric\Commented:
I usually go with   int arr[] = { 1, 2, 3 };when using initializers. Then if I add an extra initializer, I don't have to remember to change the marcro.But now you probably need to know how many elements are in the array:
    int num_elements = sizeof( arr ) / sizeof( arr[0] );

Open in new window

ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Author Commented:
>>  Do you think you may have had a mismatch earlier?

It's entirely possible  :)

>>  I usually go with ...

I ended up going with the empty brackets after I found it mentioned somewhere online. I only use C for class projects, so I always end up forgetting what I learn about it (happens every semester!).

Thanks for confirming  :)
phoffric\Commented:
C is a language. I bet it took you more than one semester to learn your native language, right? If you use it a lot, you'll speak it fluently. Good luck with your studies.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.