473,802 Members | 1,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5688
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
34400
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 this element's value happens to be 0, the conditional will return false, even though the element actually exists. Any ideas? Can you check if the element == null?
6
10268
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 off. im getting a recurring error regarding the syntax of my main() function argument calling in lottery_numbers array. so any help as to whats glaringly going wrong here?? oh and some brief guidance on whats the easiest way to sort the numbers...
6
4153
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 end is found is less efficient than using sizeof operator , since size of the array is completely determined at compile time. i don't quite understand this. Could anyone explain to me in detail ?
3
2230
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 searching, in a *base array of LONG with num elements. After some tests it seems to work well (the original version was filled with ASSERTs) (I will use this function in a bigger look-for-or-insert function) Some notes: The function returns the index...
5
3527
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 accepts a void* key argument, and uses the functions above to store this argument. It returns something (linked to the key) that the caller can store a value in. The actual key argument is always of the same pointer type (as seen in the caller,...
14
2360
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 pass the array as argument with no other info like number of strings. Is there a way to achieve that?
18
4362
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 can use dlopen/whatever to convert the function name into a pointer to that function, but actually calling it, with the right number of parameters, isn't easy. As far as I can see, there are only two solutions: 1) This one is portable. If...
7
8137
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> #include<stdlib.h> #include<math.h>
4
1680
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; I have a function that finds the maximum of an array of size 50. bool findMax50(float input)
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10538
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.