# How to find out the length of the array pointed by a pointer?

• 11-10-2012
ymc1g11
How to find out the length of the array pointed by a pointer?
Hi all,

i passed an array of integer
Code:

`int myarr[10];`
into a function like this:
Code:

```int *function(int *array, size){ ... }```
however when i tried to find out the length of myarr in the function using:
Code:

`int arrSize = sizeof(array)/sizeof(int);`
it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?

How should i find out the length of myarr in the function without passing in one more argument?

• 11-10-2012
Lesshardtofind
• 11-10-2012
root
Quote:

however when i tried to find out the length of myarr in the function using:
Code:

`int arrSize = sizeof(array)/sizeof(int);`
it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?
yes, an array isn't passe by value, there's an implicit conversion array-pointer and in the function you have a pointer to the first element, in fact in the function write array[3] or *(array+3) is the same thing.
That code works only in the function where the array is declared
Quote:

How should i find out the length of myarr in the function without passing in one more argument?
you can't, you can use many solution if you strongly need a function with 1 parameter:
1)you can declare your array of the size needed +1 and assign to the last element in position [n-1] a special value (es -9999) you're sure that the element from 0 to n-2 can't assume
2) a struct, for example:
Code:

```typedef struct{ int dim; int *a; }myarray;```

etc..
• 11-10-2012
std10093
Quote:

Originally Posted by ymc1g11
Hi all,

i passed an array of integer
Code:

`int myarr[10];`
into a function like this:
Code:

```int *function(int *array, size){ ... }```
however when i tried to find out the length of myarr in the function using:
Code:

`int arrSize = sizeof(array)/sizeof(int);`
it returns only (i think) the size of the memory location *array pointed to, which is the start of myarr?

How should i find out the length of myarr in the function without passing in one more argument?

This will give you the sizeof the pointer,which can be 2,4 or 8 depending on the system.Value 2 is very rare.

What you should do, is what you already did.Pass the size of the array as second parameter.
• 11-10-2012
c99tutorial
Quote:

Originally Posted by ymc1g11
Code:

```int *function(int *array, size){ ... }```
How should i find out the length of myarr in the function without passing in one more argument?

Simple answer: you don't. You need to somehow make the size known to the function in order for it to work. However, if you're using C99 mode then you can declare the function as follows:

Code:

```#define NELEMS(x) (sizeof((x)) / sizeof((x)[0])) void function(int size, int inArray[size], int outArray[size]) { ... }```
The parameters inArray and outArray are still passed implicitly as pointers, but C treats them as pre-initialized arrays for the purpose of the function. That means that NELEMS(inArray) will correctly give you the number of elements whether you use it inside the function or outside.
• 11-10-2012
christop
Quote:

Originally Posted by c99tutorial
The parameters inArray and outArray are still passed implicitly as pointers, but C treats them as pre-initialized arrays for the purpose of the function. That means that NELEMS(inArray) will correctly give you the number of elements whether you use it inside the function or outside.

Nope. Try it out for yourself.

The arguments are still treated as pointers, and the "array sizes" are ignored by the compiler. They're not anything like a variable-length array. The sizeof operator gives you the size of a pointer, because it is only a pointer.
• 11-10-2012
c99tutorial
Quote:

Originally Posted by christop
Nope. Try it out for yourself.

The arguments are still treated as pointers

I stand corrected on this point. sizeof(array) within the function is equal to sizeof(void*). The NELEMS macro cannot be used.

Quote:

Originally Posted by christop
and the "array sizes" are ignored by the compiler.

The sizes are not necessarily ignored. They can affect the way the array may be indexed within the function. Consider this function declaration:

Code:

```void matprint(int y, int x, int a[y][x]) {     for (int i=0; i < y; i++) {         for (int j=0; j < x; j++)             printf("%3d ", a[i][j]);         printf("\n");     } }```
Within the function, the array may be indexed as if it were a VLA of size [y][x]. If the sizes were reversed, for instance, to `int a[x][y]' then the printed result will be different if x != y.
• 11-10-2012
christop
Quote:

Originally Posted by c99tutorial
The sizes are not necessarily ignored. They can affect the way the array may be indexed within the function. Consider this function declaration:

Code:

```void matprint(int y, int x, int a[y][x]) {     for (int i=0; i < y; i++) {         for (int j=0; j < x; j++)             printf("%3d ", a[i][j]);         printf("\n");     } }```
Within the function, the array may be indexed as if it were a VLA of size [y][x]. If the sizes were reversed, for instance, to `int a[x][y]' then the printed result will be different if x != y.

The first dimension is essentially always ignored*. With a two-dimensional array parameter, as in your example, only the second dimension must be specified, so sizeof then knows how big the array (in the second dimension) is. C99 just allows the size to be variable, similar to a VLA.

*The compiler might use the dimensions for optimizations, but the first dimension doesn't otherwise affect what the code does. I actually haven't had much need for passing multidimensional arrays to functions lately, and I haven't explored the various syntaxes that were introduced in C99.