Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-31-2005, 10:41 PM
|
#1
|
Member
Registered: Nov 2002
Distribution: Slackware
Posts: 155
Rep:
|
C++ question #define ...
Hello,
I am trying to learn C++ programming, I read through some online tut... and picked up a book called "How To Program C++" by Deitel and Deitel.
But I must of missed something where ... because I am not sure I understand the following statements.
--snip--
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
n_choices = ARRAY_SIZE(choices);
--snip--
So this means anywhere I use "CTRLD" in the program it will be replaced by a init 4 ?
Next is where it gets confused, using define creates constants .. which can not be modified correct ?
So ARRAY_SIZE(a) points to (sizeof(a) / sizeof(a[0])) ... so it is like a place holder ? because in the program ARRAY_SIZE gets passed an array and simple returns the size of the array divided by 1 which is the length of the data is a[0] ?
AHHHH ......
Michael.
|
|
|
02-01-2005, 12:34 AM
|
#2
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
Quote:
#define CTRLD 4
...
So this means anywhere I use "CTRLD" in the program it will be replaced by a init 4 ?
|
Pretty much. It means that whenever CTRLD is encountered in the code, it is replaced by a literal 4. How that "4" is interpreted entirely depends on the context it's used in. In almost all cases, yes, it will be considered an integer.
Quote:
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
...
So ARRAY_SIZE(a) points to (sizeof(a) / sizeof(a[0])) ... so it is like a place holder ?
|
Not quite sure what you mean by a place holder. When a #define uses parentheses as in this case, you can think of it as defining a mini-function. The "a" in the #define is replaced with the given value in the code. So, for example, using the code you provided
Code:
ARRAY_SIZE(choices)
is equivalent to
Code:
(sizeof(choices) / sizeof(choices[0]))
Again, that's placed directly into the text of the source during the preprocessor phase. The value of the expression is the number of elements in the array, because your dividing the size of the entire array (sizeof(choices)) by the size of each element (sizeof(choices[0])). In this case, that evaluates to 5.
The choices array is made up of character pointers (each of which consumes 4 bytes of data). The array's size as a whole is the sum of all its elements. "Choice 1" is 4 bytes, "Choice 2" is 4 bytes, etc., for a total of 20 bytes for the entire array.
|
|
|
02-01-2005, 06:55 AM
|
#3
|
Member
Registered: Jan 2005
Location: Lviv, Ukraine
Distribution: Something self-made
Posts: 69
Rep:
|
try #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
|
|
|
02-01-2005, 08:27 AM
|
#4
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
What happens is, the c file goes through the macro processor ( man m4 i believe)
before compilation and all defines are replaced by the said text.
It's not rocket science, it's very basic and simple!
If you use the -E option it will just show you the pre-processor steps.
observe:
Note: this is not a valid C file, but that's OK because this happens
before actual compilation.
Code:
billym.primadtpdev>cat 1.c
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 4
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
ctrld = CTRLD
n_choices = ARRAY_SIZE(choices);
through gcc -E
Code:
billym.primadtpdev>gcc -E 1.c
# 1 "1.c"
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
};
ctrld = 4
n_choices = (sizeof( choices ) / sizeof( choices [0])) ;
good eh?

|
|
|
02-01-2005, 09:49 AM
|
#5
|
Member
Registered: Feb 2004
Posts: 47
Rep:
|
Thanks for all the replies ...
Michael.
|
|
|
All times are GMT -5. The time now is 04:57 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|