By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,917 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,917 IT Pros & Developers. It's quick & easy.

define array

P: n/a
I have a file.h that have all my definitions and I need to define a
array to use in my software, but I don´t know how can I do it.

My define´s that cause error:

....
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
....
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
....

I´d like to use FUNCTIONS_EMP in my software like a ARRAY, sample:

....
Log( ERROR_SAMPLE1 ); ==same Log( 123 );
....
Log(FUNCTIONS_EMP[1]; ==same Log( 15 );
....

Thanks for help

===========================================
Protuguês - Brasil

Eu tenho um arquivo file.h que contém todas as minhas definições e
preciso definir um array para usar no meu software, mas não estou
sabendo como fazer isto.

Minha definição que causou erro foi:

....
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
....
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
....

Eu gostaria de usar FUNCTIONS_EMP no meu software como um ARRAY,
exemplo:

....
Log( ERROR_SAMPLE1 ); ==same Log( 123 );
....
Log(FUNCTIONS_EMP[1]; ==same Log( 15 );
....

Obrigado pela ajuda.

Jul 12 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<ma********@gmail.comwrote in message
news:11*********************@s13g2000cwa.googlegro ups.com...
I have a file.h that have all my definitions and I need to define a
array to use in my software, but I don4t know how can I do it.

My define4s that cause error:

...
#define ERROR_SAMPLE1 123
#define ERROR_SAMPLE2 124
...
#define FUNCTIONS_EMP { 20, 15, 22, 53, 34)
...

I4d like to use FUNCTIONS_EMP in my software like a ARRAY, sample:

...
Log( ERROR_SAMPLE1 ); ==same Log( 123 );
...
Log(FUNCTIONS_EMP[1]; ==same Log( 15 );
...

Thanks for help
Instead of using #define for the array, you could define a global const
array in an implementation file, and declare it as extern in the header file
that other implementation files include.

-Howard

Jul 12 '06 #2

P: n/a
<ma********@gmail.comwrote (as near as I can tell, after
cleaning-up some apparent typos):

#define FUNCTIONS_EMP {20, 15, 22, 53, 34}
Log(FUNCTIONS_EMP[1]); // same as Log( 15 );

No, that's not the same thing at all! Let's look at the pre-processed
version of what you wrote (ie, what the compiler sees):

Log({20, 15, 22, 53, 34}[1]); // syntax error

Remember, now, the C preprocessor is just a text editor. It's a
rather good text editor in my opinion. I love it and use it a lot.
But none-the-less, it's just a text editor. It doesn't understand
C++ or any other language. It just processes text. You could use
it to process a novel, or a grocery list. I won't parse C++
statements, extract your meaning, and insert the correct syntax for
you. You have to do that yourself.

So, if you want to define an array, you'll have to do it directly
in C++, like so:
void foo()
{
static const int FUNCTIONS_EMP[5] = {20, 15, 22, 53, 34};
// ...
}

Now THAT will work.

(However, if FUNCTIONS_EMP will ever change, use std::vector
instead. In fact, std::vector is better than built-in arrays
for most array applications, because of bounds checking,
expandibility, member functions, etc. I personally only use
built-in arrays for very small, fixed sets of constant values.)
--
Cheers,
Robbie Hatley
East Tustin, CA, USA
lone wolf intj at pac bell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 14 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.