Thread: size of an array poited by array element

1. size of an array poited by array element

hi i have a problem with this :

Code:
char *aa[] = { "a1","a2","a3" };
char *ab[] = { "b1","b2" };
char *ac[] = { "c1","c2","c3","c4" };

char **pabc[] = { aa, ab, ac };
i want to call

Code:
int n = sizeof (pabc[0])/sizeof(char*);
to get the size of array aa.
i think i'm doing it the wrong way

thanks for help

2. No,You can't. since the type of pabc[0] is pointer to pointer to char.
sizeof(pabc[0]) = sizeof(char**)

3. no way

4. By size of array aa do you mean the number of bytes it takes up in storage or the number of elements it contains? The formula you're using looks like an alteration of a common way to get the number of elements in an array. If the scope you're in can "see" the definition of aa, you can do:
Code:
int n_elements = sizeof(aa) / sizeof(aa[0]);
int n_bytes = sizeof(aa);
If you're in some scope that can't see the definition, you're out of luck. Declare aa to be some fixed size, like so:
Code:
#define AA_LEN 3
char *aa[AA_LEN] = {"a1", "a2", "a3"};
or use a sentinel value (null terminate it) and write a function to get the length:
Code:
char *aa[] = {"a1", "a2", "a3", NULL};
int strv_len(char *aa[])
{
int i;
for (i = 0; aa[i]; i++);
return i;
}

5. Originally Posted by anduril462
By size of array aa do you mean the number of bytes it takes up in storage or the number of elements it contains?
yes, i want to check the number of elements and thank you for your reply.

6. Originally Posted by mitofik
yes, i want to check the number of elements and thank you for your reply.
You have to keep track of that on your own.

Quzah.

7. i don't know what your means ,but you can try fthe code as following
Code:
int n = sizeof(pabc) / sizeof(pabc[0]);

it get the number of a array .