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

arrays again :-)

look this code:

#include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0])

void func(int arr[]){
printf("func(): length(arr) = %d\n", length(arr));
}

void main(void){
int i, arr[] = {1,2,3};
printf("main(): length(arr) = %d\n", length(arr));
func(arr);
}

and, please, explain me, why length() macro
works with differences in different context?
stupid question? oh...

Feb 16 '06 #1
5 1219
al3x4nder wrote:
look this code:

#include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0])

void func(int arr[]){
As is well-known, declaring a function argument as a simple array is
treated as declaring it as a /pointer/ argument, so that this is the
same as writing `void func( int *arr )`.
printf("func(): length(arr) = %d\n", length(arr));
Hence this will /not/ print the length of the array.
}
void main(void){


Declaring `main` as returning `void` is unnecesarily non-portable;
don't do it.

--
Chris "was stirred, now shaken" Dollin
RIP Andreas "G'Kar" Katsulas, May 1946 - February 2006
Feb 16 '06 #2
al3x4nder wrote:
look this code:

#include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0])

void func(int arr[]){
printf("func(): length(arr) = %d\n", length(arr));
}

void main(void){
int i, arr[] = {1,2,3};
printf("main(): length(arr) = %d\n", length(arr));
func(arr);
}

and, please, explain me, why length() macro
works with differences in different context?
stupid question? oh...


When you pass an array as a parameter to a function you're really
passing a pointer to its first element. In fact when an array is used
in an expression except as the operand of the sizeof or the & operator,
the array identifier allways decays into a pointer to the first element
of the array, including when the arrays is one parameter in a function
call.

The declaration of func you gave is totally equivalent to:

void func (int *arr)
[...]

Inside the function arr is of type int*, not of type int[3]. So inside
the function, the macro evaluates to:

length(arr) = sizeof(arr) / sizeof(arr[0]) = sizeof (int*) / sizeof
(int) = some value that will vary from implementation to
implementation. 1 & 2 are usual values, but you can expect about
anything.

In main the macro expands to:

length(arr) = sizeof(arr) / sizeof(arr[0]) = sizeof (int[3]) / sizeof
(int) = 3 irregardless of implementation

HTH

Feb 16 '06 #3
al3x4nder wrote:
look this code: #include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0]) void func(int arr[]){
printf("func(): length(arr) = %d\n", length(arr));
} void main(void){
int i, arr[] = {1,2,3};
printf("main(): length(arr) = %d\n", length(arr));
func(arr);
}


Within func, arr is of type int* and it's size is that of that pointer,
not of that of the array. You will have to pass the length of the array
along with it I'm afraid. That's one of the reasons c strings are null
terminated: That's the way to determine the length of that array.

The output of func is platform dependet. I guess it will usually be 1 or
2.

Jens

Feb 16 '06 #4
al3x4nder wrote:
look this code:

#include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0])
The defensive approach is

#define length(arr) (sizeof (arr) / sizeof ((arr)[0]))
void func(int arr[]){
printf("func(): length(arr) = %d\n", length(arr));
}


Only a pointer to the first element of arr is passed to func; the length
needs to be passed separately. That's why you see function signatures
such as

int sum(int a[], int len);
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.
Feb 16 '06 #5

August Karlstrom написав:
al3x4nder wrote:
look this code:

#include <stdio.h>
#define length(arr) sizeof(arr)/sizeof(arr[0])


The defensive approach is

#define length(arr) (sizeof (arr) / sizeof ((arr)[0]))
void func(int arr[]){
printf("func(): length(arr) = %d\n", length(arr));
}


Only a pointer to the first element of arr is passed to func; the length
needs to be passed separately. That's why you see function signatures
such as

int sum(int a[], int len);
August

--
I am the "ILOVEGNU" signature virus. Just copy me to your
signature. This email was infected under the terms of the GNU
General Public License.


thanks

Feb 16 '06 #6

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

Similar topics

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 >...
21
by: Matteo Settenvini | last post by:
Ok, I'm quite a newbie, so this question may appear silly. I'm using g++ 3.3.x. I had been taught that an array isn't a lot different from a pointer (in fact you can use the pointer arithmetics to...
9
by: Charles Banas | last post by:
i've got an interesting peice of code i'm maintaining, and i'd like to get some opinions and comments on it, hopefully so i can gain some sort of insight as to why this works. at the top of the...
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
53
by: SK | last post by:
Hi I appreciate all of the feedback I have recieved. I am enjoying working on my program and I hope that it can be appreciated. I have my program compiling now and I am continuing to work out...
33
by: Peter Seaman | last post by:
I understand that structures are value types and arrays and classes are reference types. But what about arrays as members of structures i.e. as in C struct x { int n; int a; }
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
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
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.