Get array size by a pointer dynamically

T

terry

Hi,

Could anyone tell me how to determine the size of array of characters
dynamically? For example,

:
:
char *a[]={"hello","hi","kitty"};
char *b[]={"orange","apple"};

void main()
{
char **p=a;
:
:

}

I want to get array size of "a" which is 3 by "p". How?

Thanks!
 
J

Joona I Palaste

terry said:
Could anyone tell me how to determine the size of array of characters
dynamically?

Impossible in ISO standard C.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
T

Trent Buck

Quoth terry on or about 2004-11-14:
Could anyone tell me how to determine the size of array of characters
dynamically? For example,

char *a[]={"hello","hi","kitty"};

I want to get array size of "a" which is 3 by "p". How?

The usual method is to make the last element in the array a `special'
value, such as 0 or -1 or EOF. For example (caveat: untested)

void
main (void)
{
char *x[] = { "foo", "bar", "baz", 0 };
int idx, len;

len = 0;
for (idx = 0; x[idx]; ++idx)
len++;

printf ("Length is `%d'\n", len);
}

-trent
 
K

Kurt Watzka

Trent said:
Quoth terry on or about 2004-11-14:
Could anyone tell me how to determine the size of array of characters
dynamically? For example,

char *a[]={"hello","hi","kitty"};

I want to get array size of "a" which is 3 by "p". How?

The usual method is to make the last element in the array a `special'
value, such as 0 or -1 or EOF. For example (caveat: untested)

Why do you think this is the "usual" method? The usual method is to pass
the size of the array along with a ponter to the first element. To avoid
counting the elements in the array, you can use sizeof, both on the array
and on its first element (sizeof a/sizeof a[0]), if the initialization of
the array is visible.

However, if the number of elements in "a" is determined by an
initializer, there is nothing dynamic about it.
void
main (void)

This _may_ cause a compiler targeting a hosted environment to optimize away
the whole program, or to put "bam" in x[3].
{
char *x[] = { "foo", "bar", "baz", 0 };
int idx, len;

len = 0;
for (idx = 0; x[idx]; ++idx)
len++;

printf ("Length is `%d'\n", len);
}

Kurt Watzka
 
L

Lawrence Kirby

Hi,

Could anyone tell me how to determine the size of array of characters
dynamically? For example,

:
:
char *a[]={"hello","hi","kitty"};
char *b[]={"orange","apple"};

void main()

In C main returns int.
{
char **p=a;
:
:

}

I want to get array size of "a" which is 3 by "p". How?

In C a pointer doesn't know anything about the size of an array it is
pointing at. This is fundamental and means that even various standard
library functions that need to know the size of an array require you to
pass that size (e.g. fgets()). So the direct answer to your question to
your question is that you can't.

As others have said there are various techniques to work around this, e.g.
putting a marker in the array data or maintaining a size variable
alongside the pointer. And this is an issue specifically with pointers, if
you have the ARRAY definition available you can get the size off that e.g.
(sizeof a/sizeof *a) will evaluate to 3 for the definition of a above.

Lawrence
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top