arrays again :-)

A

al3x4nder

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...
 
C

Chris Dollin

al3x4nder said:
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.
 
A

Antonio Contreras

al3x4nder said:
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
 
J

Jens Theisen

al3x4nder said:
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
 
A

August Karlstrom

al3x4nder said:
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
 
A

al3x4nder

August Karlstrom напиÑав:
al3x4nder said:
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

thanks
 

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
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top