determine size of array if passed to function

how to find size of dynamic array in c
how to find length of array in c without using sizeof
find size of array passed to function c++
sizeof(array c++)
can the sizeof operator be used to tell the size of an array passed to a function
array size
c++ int array length
sizeof array of structs

Is it possible to determine the size of an array if it was passed to another function (size isn't passed)? The array is initialized like int array[] = { XXX } ..

I understand that it's not possible to do sizeof since it will return the size of the pointer .. Reason I ask is because I need to run a for loop inside the other function where the array is passed. I tried something like:

for( int i = 0; array[i] != NULL; i++) {
........
}

But I noticed that at the near end of the array, array[i] sometimes contain garbage values like 758433 which is not a value specified in the initialization of the array..


The other answers overlook one feature of c++. You can pass arrays by reference, and use templates:

template <typename T, int N>
void func(T (&a) [N]) {
    for (int i = 0; i < N; ++i) a[i] = T(); // reset all elements
}

then you can do this:

int x[10];
func(x);

but note, this only works for arrays, not pointers.

However, as other answers have noted, using std::vector is a better choice.

How to print size of array parameter in C++?, However, using the sizeof operator to determine the size of arrays is error prone. The function has one parameter declared as int array[] and is passed a static� There is no 'built-in' way to determine the length inside the function. However you pass arr, sizeof(arr) will always return the pointer size. So the best way is to pass the number of elements as a seperate argument. Alternatively you could have a special value like 0 or -1 that indicates the end (like it is \0 in strings, which are just char []).


If it's within your control, use a STL container such as a vector or deque instead of an array.

ARR01-C. Do not apply the sizeof operator to a pointer , The standard way is to use sizeof operator to find size of a C-style array. We know that array decays into a pointer when passed to a function as an argument � Not possible. You need to pass the size of the array from the function, you're calling this function from. When you pass the array to the function, only the starting address is passed not the whole size and when you calculate the size of the array, Compiler doesn't know How much size/memory, this pointer has been allocated by the compiler. So, final call is, you need to pass the array size while you're calling that function.


Nope, it's not possible.

One workaround: place a special value at the last value of the array so you can recognize it.

Find size of an array in C using sizeof() and pointer arithmetic, We know that an array decays into a pointer when passed to a function as an get the length of an array inside a function in C++, the recommended solution is� array.length: length is a final variable applicable for arrays. With the help of the length variable, we can obtain the size of the array. Examples: int size = arr[].length; // length can be used // for int[], double[], String[] // to know the length of the arrays.


One obvious solution is to use STL. If it's not a possibility, it's better to pass array length explicitly. I'm skeptical about use the sentinel value trick, for this particular case. It works better with arrays of pointers, because NULL is a good value for a sentinel. With array of integers, it's not that easy - you need to have a "magic" sentinel value, which is not good.

Side note: If your array is defined and initalized as

 int array[] = { X, Y, Z };

in the same scope as your loop, then

sizeof(array) will return it's real size in bytes, not the size of the pointer. You can get the array length as

sizeof(array) / sizeof(array[0])

However, in general case, if you get array as a pointer, you can't use this trick.

Find length of an array in C++ in 5 different ways, For example, suppose we want to write a function to return the last element of an array will decay to a pointer, so the length must be passed separately */ int last It is a very common error to attempt to determine array size from a pointer,� array::size() in C++ STL; C Program to find size of a File; How to print size of array parameter in C++? C/C++ program to find the size of int, float, double and char; Generate an array of given size with equal count and sum of odd and even numbers; size of char datatype and char array in C; Get the stack size and set the stack size of thread attribute in C


You could add a terminator to your int array then step through the array manually to discover the size within the method.

#include<iostream>
using namespace std;

int howBigIsBareArray(int arr[]){
    int counter = 0;
    while (arr[counter] != NULL){
        counter++;
    }
    return counter;
}
int main(){
    int a1[6] = {1,2,3,4,5,'\0'};
    cout << "SizeOfMyArray: " << howBigIsBareArray(a1);
}

This program prints:

SizeOfMyArray: 5

This is an O(n) time complexity operation which is bad. You should never be stepping through an array just to discover its size.

C Language, However, it seems that an array parameter is passed and treated as just another So inside the function sizeof() will calculate size of first element of array a. sz = size (A) returns a row vector whose elements are the lengths of the corresponding dimensions of A. For example, if A is a 3-by-4 matrix, then size (A) returns the vector [3 4]. If A is a table or timetable, then size (A) returns a two-element row vector consisting of the number of rows and the number of table variables.


'sizeof(a)/sizeof(a[0])' work for an array passed as a , To pass an array as a parameter to a function, pass it as a pointer (since it is a pointer). For example Most often, you find that you want to pass the logical size. The variable len stores the length of the array. The length is calculated by finding size of array using sizeof and then dividing it by size of one element of the array. Then the value of len is displayed. The code snippet for this is given as follows −


Passing arrays to functions, Something like: sizeof(array)/sizeof(char) only works if the code is inside a function, because the compiler treats the passed in array as a� #include <stdio.h> void myfuncn( int *var1, int var2) { /* The pointer var1 is pointing to the first element of * the array and the var2 is the size of the array. In the * loop we are incrementing pointer so that it points to * the next element of the array on each increment.


any way to determine size of array thats a function , A potential problem with this approach is that it is easy to screw up by forgetting the array to pointer decay rule or by passing the wrong size� Something like: sizeof(array)/sizeof(char) only works if the code is executed in main and the array is declared inside of main sizeof(array)/sizeof(char) does not work inside a function, because the compiler treats the passed in array as a pointer, and returns the size of the pointer, not the array.