473,396 Members | 1,968 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,396 software developers and data experts.

Arrays as function parameters and sizeof()

Hi,

when I had in mind to turn this code (which worked fine in main()) to a
separate function, I stumbled upon this...(of course, it's way bigger than
that, but I hope this short snippet can demonstrate the issue anyway)

-begin code (Ì) -
int main(void)
{
unsigned char rawdata[96] =
{0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */

unsigned long x = 0;
int i;

for(i=0; i < sizeof(rawdata)/(sizeof(unsigned long)); i++)
{
...
x = ((rawdata[i*4+0] << 8) | .....;
/* ... */
}
return 0;
}
-end code (I)-

The for() line contains an i < sizeof(rawdata). In main(), this causes no
problems, however, if we (commonly) assume that the function-internal
variables are completely different, we *may* run into trouble...
-begin code (II)-
unsigned long doMagic (unsigned char[]);

int main(void)
{
unsigned char rawdata[64] = {
0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */

unsigned long x = doMagic(rawdata);
... /* e. g. printf output */
return 0;
}

unsigned long doMagic (unsigned char arr[])
{
int i;
for(i=0; i < sizeof(arr)/(sizeof(unsigned long)); i++)
{
...
x = ((arr[i*4+0] << 8) | ....... ;
}
}
-end code (II)
For an absolute beginner, this looks "working fine", but it does not. :)
sizeof(arr) is *NOT* the size of the array (just as I too thought before),
but merely the size of the small pointer (here it was 4). This caused the
for loop to get exited prematurely.

The only way out with the portable solution was to write a different
doMagic():

unsigned long doMagic (unsigned char arr[], unsigned int len);

and then *externally* enter the function using sizeof(rawdata) as length
"info".
This worked fine.

But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it "pre-chewed" by
the calling routine?

-Andreas

Oct 31 '08 #1
3 2964
Andreas Eibach wrote:
[... sizeof function parameter declared as array ...]
But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it "pre-chewed" by
the calling routine?
No. Although you can write the parameter as if it were an
array, in truth it is a pointer to an element of the array. So
sizeof produces the size of a pointer, not the size of the thing
pointed to nor the size of the whole array of things.

See Question 6.21 -- in fact, most of Section 6 -- in the
comp.lang.c Frequently Asked Questions (FAQ) list

http://www.c-faq.com/

See also the rest of the FAQ; it's nutritious.

--
Er*********@sun.com
Oct 31 '08 #2

"Eric Sosman" <Er*********@sun.comwrote:
Andreas Eibach wrote:
[... sizeof function parameter declared as array ...]
But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it
"pre-chewed" by
the calling routine?

No. Although you can write the parameter as if it were an
array, in truth it is a pointer to an element of the array. So
sizeof produces the size of a pointer, not the size of the thing
pointed to nor the size of the whole array of things.

See Question 6.21 -- in fact, most of Section 6 -- in the
comp.lang.c Frequently Asked Questions (FAQ) list
Well I'll be DAMNED.
A zillion FAQs in the net with having most stuff in main() and using a hell
of global variables, but only this one covers *that* very problem.
I stand corrected. Yes that would've been the answer I'd needed. Thanks.
Bookmarked.

-Andreas

Oct 31 '08 #3
On Oct 31, 11:37 pm, "Andreas Eibach" <aeib...@mail.comwrote:

<snip>
For an absolute beginner, this looks "working fine", but it does not. :)
sizeof(arr) is *NOT* the size of the array (just as I too thought before),
but merely the size of the small pointer (here it was 4). This caused the
for loop to get exited prematurely.

The only way out with the portable solution was to write a different
doMagic():

unsigned long doMagic (unsigned char arr[], unsigned int len);

and then *externally* enter the function using sizeof(rawdata) as length
"info".
This worked fine.

But ... is there no other way to *compute* the actual size of the array
(here 96) *inside* the function without expecting to have it "pre-chewed" by
the calling routine?
There's some ways.
You said the rest is intented to be 0:
int main(void)
{
unsigned char rawdata[96] =
{0x32, 0xA7, 0x53, 0x00, 0xE0, 0x20, 0x0F, 0x19,
0x00, 0x00, 0x03, 0x70, 0x43, 0xFA, 0x00, 0x18,
0x4E, 0xAE, 0xFF, 0xA0, 0x4A, 0x80, 0x67, 0x0A
}; /* note, rest is _intended_ to be zeroes */
unsigned long doMagic (unsigned char arr[])
{
int i;
for(i=0; i < sizeof(arr)/(sizeof(unsigned long)); i++)
So you can have an end marker there.
Ie

unsigned char rawdata[N + 4] = { /* ... */ [N] = 1, [N + 1] = 1, [N +
2] = 1, [N + 3] = 1 }; /* [N] = is C99 only */

Then you can break the loop when you encounter those intentional
zeroes, and then you read something that is not zero.
Note your code makes assumptions about the size of unsigned long and
the representation of it. (notice the end marker example code I gave
suffers from the same issues)
*(unsigned long *)rawdata may be a trap representation for example
Oct 31 '08 #4

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

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
4
by: Paul David Buchan | last post by:
Hello, I'm attempting to write a program to read in database files (.dbf). When I do it all as a single procedure in main, everything works. However, what I really want, is to pass the database...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.