LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-31-2005, 10:41 PM   #1
Bluesuperman
Member
 
Registered: Nov 2002
Distribution: Slackware
Posts: 155

Rep: Reputation: 30
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.
 
Old 02-01-2005, 12:34 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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.
 
Old 02-01-2005, 06:55 AM   #3
pvs
Member
 
Registered: Jan 2005
Location: Lviv, Ukraine
Distribution: Something self-made
Posts: 69

Rep: Reputation: 16
try #define ARRAY_SIZE(a) (sizeof((a)) / sizeof((a)[0]))
 
Old 02-01-2005, 08:27 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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?
 
Old 02-01-2005, 09:49 AM   #5
michael_util
Member
 
Registered: Feb 2004
Posts: 47

Rep: Reputation: 15
Thanks for all the replies ...

Michael.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Best way to define variables? Yig Linux - Software 3 08-24-2005 05:09 PM
Question: How to define an SQL HAVING clause in relational algebra? jdruin Linux - Software 1 11-08-2004 07:50 AM
define jhon Linux - Networking 2 08-30-2004 07:42 PM
question on #define h/w Programming 7 12-03-2003 05:14 PM
using #define Jo_Nak Programming 4 06-11-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:52 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration