ProgrammingThis 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.
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.
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] ?
#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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.