473,386 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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

Feb 3 '06 #1
3 5668
shyam wrote:

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.
--
Ian Collins.
Feb 3 '06 #2
shyam wrote:
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]));
}

--
Nick Keighley

Feb 3 '06 #3
shyam wrote:
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.

Feb 3 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Noah Spitzer-Williams | last post by:
Hello guys, I would like to do something seemingly simple: find out if an element in an array that is passed to my function exists. I used to think I could just do: if (arr) ... However, if...
6
by: moi | last post by:
hi, i have an assignment to put 6 pseudo random numbers into an array to simulate drawing 6 lottery numbers between 1-49. the code i have to do this so far is listed below. i dont think its far...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: Massimiliano Alberti | last post by:
Can someone check this? If it's OK, you can use however you want... :-) It should search for an element in an array and, if it can't find it, return the next element. key is what you are...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
14
by: ranjmis | last post by:
Hi all, Below is the code wherein I am initializing double dimentional array inside main with string literals. Now I want to display the strings using a function call to which I just want to...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
4
by: lightaiyee | last post by:
Dear Gurus, I would like to implement a function that computes the number of times a certain condition is met in a global array. For example, I have an global array of size 500. float array;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.