Initialization of pointer arrays

G

gnat

Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler
will count the initializers and fill in the correct number. How
do i find out what number the compiler has chosen? how do i find
out what the array size is so i don't overflow it by accident?

I have taken to putting a string ("000000") at the end and
checking for it when i loop through it stopping when i find it,
but i think that there must be a better way.
 
M

Martin Dickopp

gnat said:
Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler will count
the initializers and fill in the correct number. How do i find out what
number the compiler has chosen?

sizeof name / sizeof *name

IOW, the size of an array divided by the size of a single element of
that array is the number of array elements.

Martin
 
G

gnat

Martin said:
Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler will count
the initializers and fill in the correct number. How do i find out what
number the compiler has chosen?


sizeof name / sizeof *name

IOW, the size of an array divided by the size of a single element of
that array is the number of array elements.

Martin

Will that be flummoxed by the fact that each string is a
different length?
 
A

Alex Monjushko

gnat said:
Hello comp.lang.c,
given the code:
static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};
Since i have not specified the size of the array the compiler
will count the initializers and fill in the correct number. How
do i find out what number the compiler has chosen? how do i find
out what the array size is so i don't overflow it by accident?
I have taken to putting a string ("000000") at the end and
checking for it when i loop through it stopping when i find it,
but i think that there must be a better way.

You have the right general idea. The traditional way to do this is to
NULL-terminate your array, such as:

static char *name[] = { "Bob",
"Jane",
"Superman",
"Joe Sixpack",
NULL };

However, if NULL is a valid in the context of your array then you will
need to chose a different terminator.
 
A

Alex Monjushko

gnat said:
Martin Dickopp wrote:
Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler will count
the initializers and fill in the correct number. How do i find out what
number the compiler has chosen?


sizeof name / sizeof *name

IOW, the size of an array divided by the size of a single element of
that array is the number of array elements.

Martin
Will that be flummoxed by the fact that each string is a
different length?

No. You have declared an array of pointers to char, which are all of
the same size.
 
G

gnat

Alex said:
gnat said:
Martin Dickopp wrote:
Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler will count
the initializers and fill in the correct number. How do i find out what
number the compiler has chosen?


sizeof name / sizeof *name

IOW, the size of an array divided by the size of a single element of
that array is the number of array elements.

Martin

Will that be flummoxed by the fact that each string is a
different length?


No. You have declared an array of pointers to char, which are all of
the same size.

ah, i see it now (sometimes things need to be painfully obvious ^_^).

Thank you both.
 
M

Martin Dickopp

gnat said:
Martin said:
gnat said:
Hello comp.lang.c,

given the code:

static char *name[] = {
"Bob", "Jane", "Superman", "Joe Sixpack"
};

Since i have not specified the size of the array the compiler will count
the initializers and fill in the correct number. How do i find out what
number the compiler has chosen?
sizeof name / sizeof *name
IOW, the size of an array divided by the size of a single element of
that array is the number of array elements.

Will that be flummoxed by the fact that each string is a different
length?

No, that's not a problem. The string literals themselves are not in the
array; they are somewhere in memory. The array consists of pointers to
the first elements of each of the string literals.

`sizeof *name' is therefore identical to `sizeof (char *)', i.e. it
is the size of a pointer to `char'. The size of such a pointer is
naturally independent of what the pointer actually points to.

Martin
 

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,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top