1

I know how to detect size of array after I created it by the help of sizeof paramater:

int arr[10];
printf("%i",sizeof(arr)/sizeof(int));

But as I understand, when I pass an array as a parameter to a function, Im acctually passing a pointer to the first element of the array, and not the actual array. So if I will use the sizeof operator I will get the size of the pointer.

I know I can find a size of a string(char array) by searching for '\0'. So lets assume we are talking about int array;

How can I detect the size of an array passed as argument?

Shelef
  • 3,707
  • 6
  • 23
  • 28

7 Answers7

3

As usual, when this question is asked, a multitude of answers appear saying that you must pass the number of elements as a parameter to the function.

This is, of course, incorrect. If the function needs to know the number of elements in an array, you must provide the information to the function, but it can be provided in a variety of ways. This is most simply via a parameter, but it might be via any combination of zero or more parameters, global state, information pointed directly or indirectly by parameters or global state, static information remembered from previous function calls, some quality in the array itself (such as a sentinel), compile-time constants, or pretty much any other means imaginable, and the number might be explicitly present in any of the aforementioned or it might be calculated or deduced from information.

About the only way you cannot get the number of elements in the array is via a standard C mechanism applied to the pointer that is passed.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

You can't, you have to send the dimensions as additional parameters to the function.

Joe
  • 7,378
  • 4
  • 37
  • 54
1

You can't. In C, when you pass an array to function, it decays from an array to a pointer-to-type, so far as the function's scope is concerned.

So, consider this:

int arr[16];
sizeof(arr);          // sizeof WORKS
myFunction(arr);      // Array decays to pointer, sizeof FAILS inside function
myFunction(&arr[0]);  // Array decays to pointer, sizeof FAILS inside function

void myFunction(int* ptr)
{
  // sizeof(ptr) will not give you what you would expect normally
}

The traditional way to do this in C is to have an extra function parameter, ie:

void myFunction(int* ptr, size_t mySize)
{
}

Or, create your own structure that holds the pointer to the array and it's size, ie:

typedef struct myType {
  int*   ptrToArray;
  size_t arraySize;
} myType_t;

int array[16] = { 0 };
myType_t data;
data.ptrToArray = array;
data.arraySize = 16;

void myFunction(myType_t data)
{
}
Cloud
  • 18,753
  • 15
  • 79
  • 153
0

By either null-terminating the array, or by passing in the number of elements. There is no way to tell how many elements are contained in an array in C.

user229044
  • 232,980
  • 40
  • 330
  • 338
0

You need to have it passed as a parameter to the function. Or you need to declare the size of the array to be passed as fixed...

void myFunc( int* array, size_t numElements );

or if the size of the array is a known constant

void myFunc( int array[10] );

or any of the other myriad ways to make the size of the array available to the function (globals, etc.). It is not a built in function of C to detect the size of an array via its pointer.

K Scott Piel
  • 4,320
  • 14
  • 19
0

You can NULL terminate the array and loop through incrementing a variable each time until it reaches NULL this will give you the size.

int arr[10] = {1, 2, 3, 4, 5, NULL}

or something like that. In your case you would probably have to write NULL to current index + 1. The other options like creating a struct that holds the size are probably better as they cost less.

0

Contrary to what you might expect the following program exits with code 1 (also @K Scott Piel)

int f(int a[10])
{
        return sizeof(a)/sizeof(int);
}

int main(void)
{
        int     a[10];
        return f(a);
}

This is indeed as a couple others have mentioned: because a parameter array in C is just a pointer. You could check your compiler, maybe it has extensions to bypass it, but it will make your code unportable.

A way to come around it, is by wrapping the array inside a struct. The following program does exit with code 10.

typedef struct { int a[10]; } a10;
int f(a10 a)
{
        return sizeof(a)/sizeof(int);
}

int main(void)
{
        a10     a;
        return f(a);
}

It's a bit more of a hassle to get at the elements, though. If you want to support varying sizes of arrays, you'll have to program it yourself (as others have pointed out). Please be aware that sizeof is computed at compile time and is a constant as far as the compiler is concerned. It can only vary from platform to platform.

Oh and note that the array in the second example is passed by value whereas it is passed by reference in the first.

Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18