How to find number of array elements in a function argument.

S

shyam

Hello to all C geeks

My query is as follows

i declare a variable in main as
UINT8 *c[5]

if i check for sizeof(c) it returns 20, i.e 20/4 = 5 elements.

This is fine .

Now i have a function

void func(UINT8 **s,/*other args*/)

and i call this function from main passing c as first argument for s.
Now my problem is that inside the func when i call sizeof(s) it shows 4

i.e the size of one char pointer.

So there is no way for me to know exactly how many elements are there
in the argument.

I have a feeling that it is not possible in C as it dosnt support RTTI
stuff and all but
i will appreciate if anyone can help me or give me some alternative.

thanks in advance & with best regards

shyam
 
I

Ian Collins

shyam said:
Now i have a function

void func(UINT8 **s,/*other args*/)

and i call this function from main passing c as first argument for s.
Now my problem is that inside the func when i call sizeof(s) it shows 4

i.e the size of one char pointer.

So there is no way for me to know exactly how many elements are there
in the argument.
No, this was asked here last week, go back and have a look at the
replies on that thread.
 
N

Nick Keighley

shyam said:
i declare a variable in main as
UINT8 *c[5]

if i check for sizeof(c) it returns 20, i.e 20/4 = 5 elements.

This is fine .

Now i have a function

void func(UINT8 **s,/*other args*/)

and i call this function from main passing c as first argument for s.
Now my problem is that inside the func when i call sizeof(s) it shows 4

i.e the size of one char pointer.

So there is no way for me to know exactly how many elements are there
in the argument.

no, you'll have to pass it.
I have a feeling that it is not possible in C as it dosnt support RTTI
stuff and all but
i will appreciate if anyone can help me or give me some alternative.

void func (UINT8 **s, size_t size)

and call it:

int main (void)
{
UINT8 *c[5]
func (&c, sizeof(c) / sizeof(c [0]));
}
 
A

Antonio Contreras

shyam said:
Hello to all C geeks

My query is as follows

i declare a variable in main as
UINT8 *c[5]

if i check for sizeof(c) it returns 20, i.e 20/4 = 5 elements.

This is fine .

Now i have a function

void func(UINT8 **s,/*other args*/)

and i call this function from main passing c as first argument for s.
Now my problem is that inside the func when i call sizeof(s) it shows 4

i.e the size of one char pointer.

Actually, given your declaration it would be the size of a pointer to
pointer to char.
So there is no way for me to know exactly how many elements are there
in the argument.

I have a feeling that it is not possible in C as it dosnt support RTTI
stuff and all but
i will appreciate if anyone can help me or give me some alternative.

thanks in advance & with best regards

Basically you're right. There's no way to determine the length of an
array that gets passed to a function as an argument. As others have
pointed out, this question has been asked repeteadly in this newsgroup.
There are a number of ways to work arround this problem, google the
newsgroups a bit and you should find some.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top