Query re: C and a define that has me confused

N

No_One

Ran across the following in some source code while learning ncurses.

#include <curses.h>
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))


char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
n_choices = ARRAY_SIZE(choices);


Now my question is, the define is dividing the size of the array choices by
the size of the first eleemnt of that array to get the number of elements in
the array.

Right ?

If so then wouldn't this only work if all the elements were the same size???

ken
 
G

Gordon Burditt

#include said:
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))


char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
n_choices = ARRAY_SIZE(choices);


Now my question is, the define is dividing the size of the array choices by
the size of the first eleemnt of that array to get the number of elements in
the array.

Right ?

If so then wouldn't this only work if all the elements were the same size???

The elements of an array *ARE* the same size.

In this case, the elements of the array are pointers-to-char, and
they are all the same size. The size of the strings being pointed
at don't enter into it.

Gordon L. Burditt
 
M

Mike Wahler

No_One said:
Ran across the following in some source code while learning ncurses.

#include <curses.h>
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))


char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
n_choices = ARRAY_SIZE(choices);


Now my question is, the define is dividing the size of the array choices
by
the size of the first eleemnt of that array to get the number of elements
in
the array.

Right ?

If so then wouldn't this only work if all the elements were the same
size???

Yes, and they are the same size. That is, sizeof(char*).


-Mike
 
C

Chris Croughton

Ran across the following in some source code while learning ncurses.

#include <curses.h>
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))


char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,
};
n_choices = ARRAY_SIZE(choices);


Now my question is, the define is dividing the size of the array choices by
the size of the first eleemnt of that array to get the number of elements in
the array.

Right ?

Correct, I have much the same macro in a lot of my code.
If so then wouldn't this only work if all the elements were the same size???

Array elements are always the same size as each other within an array.
Effectively:

a == *(a + i) == *(type_of a[0])((char *)&a[0] + i * sizeof(a[0]))

The things in your array are pointers to char strings, those pointers
are all the same size. What they point to isn't, but you aren't indexing
that...

(If you wanted to have an array of strings rather than pointers to them
you'd need to do something like char choices[][STRING_SIZE], where
STRING_SIZE is a constant, and so choices[] is still an array of things
of a constant size.)

Chris C
 
N

No_One

I want to thank everyone for the detailed information, now I understand.

Again, thanks for the help, it is appreciated.

ken
 
E

Emmanuel Delahaye

No_One wrote on 22/04/05 :
Ran across the following in some source code while learning ncurses.

#include <curses.h>
#include <menu.h>

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

Correct, could be written

#define ARRAY_SIZE(a) (sizeof(a) / sizeof *(a))

too...
char *choices[] = {
"Choice 1",
"Choice 2",
"Choice 3",
"Choice 4",
"Exit",
(char *)NULL,

Useless cast.
n_choices = ARRAY_SIZE(choices);

Now my question is, the define is dividing the size of the array choices by
the size of the first element of that array to get the number of elements in

By the size of *any* element.
the array.

Right ?
Yes.

If so then wouldn't this only work if all the elements were the same size???

By definition, an array is a sequence of objects with the same size
(here, a pointer, whatever the pointed data size)

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,162
Messages
2,570,896
Members
47,434
Latest member
TobiasLoan

Latest Threads

Top