Determine Size of Array If Passed to Function

determine size of array if passed to function

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.

Find the Size of integer array received as an argument to a function in c

You cannot do it that way. When you pass an array to a function, it decays into a pointer to the first element, at which point knowledge of its size is lost.

If you want to know the size of an array passed to the function, you need to work it out before decay and pass that information with the array, something like:

void function (size_t sz, int *arr) { ... }
:
{
int x[20];
function (sizeof(x)/sizeof(*x), x);
}

How can we get the size of an array that is passed into the function?

Of course you can always use a template:

#include <iostream>
using namespace std;

template<typename T, std::size_t N>
void findFrequency (T(&input1)[N], int input2)
{
// input1 is the array
// N is the size of it

//int n = sizeof(input1) / sizeof(input1[0]);

int count = 0;
for(int i = 0; i < N; i++)
{
if(input1[i] == input2)
count++;
}
string ch;
if(count == 0)
ch = to_string(input2) + " not present";
else
ch = to_string(input2) + " comes " + to_string(count) + " times";
std::cout << ch << "\nn = " << N;
}
int main ()
{
int a[] = {1, 1, 3, 4, 5, 6};
findFrequency(a, 1);
}

How do I determine the size of my array in C?

Executive summary:

int a[17];
size_t n = sizeof(a)/sizeof(a[0]);

Full answer:

To determine the size of your array in bytes, you can use the sizeof
operator:

int a[17];
size_t n = sizeof(a);

On my computer, ints are 4 bytes long, so n is 68.

To determine the number of elements in the array, we can divide
the total size of the array by the size of the array element.
You could do this with the type, like this:

int a[17];
size_t n = sizeof(a) / sizeof(int);

and get the proper answer (68 / 4 = 17), but if the type of
a changed you would have a nasty bug if you forgot to change
the sizeof(int) as well.

So the preferred divisor is sizeof(a[0]) or the equivalent sizeof(*a), the size of the first element of the array.

int a[17];
size_t n = sizeof(a) / sizeof(a[0]);

Another advantage is that you can now easily parameterize
the array name in a macro and get:

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);

How do you get the size of array that is passed into the function?

You need to also pass the size of the array to the function.

When you pass in the array to your function, you are really passing in the address of the first element in that array. So the pointer is only pointing to the first element once inside your function.

Since memory in the array is continuous though, you can still use pointer arithmetic such as (b+1) to point to the second element or equivalently b[1]

void print_array(int* b, int num_elements)
{
for (int i = 0; i < num_elements; i++)
{
printf("%d", b[i]);
}
}

This trick only works with arrays not pointers:

sizeof(b) / sizeof(b[0])

... and arrays are not the same as pointers.

Using sizeof() on an array passed to a function

Array in C always passed by reference. Thats why you are getting pointer size each time, not actual size.

To work with array in C as an argument, you should pass size of array with array.

I modified your program to working condition:

typedef unsigned char BYTE;

void checkArraySize(BYTE data[], int sizeOfArray)
{
int internalSize = sizeOfArray;
printf("%d", internalSize );
}

void main(void)
{
BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
int externalSize = sizeof(data)/sizeof(BYTE); //it would return number of elements in array

checkArraySize(data, externalSize);
}

passed by reference means only address of first element of array is sent. If you change anything even inside function checkArraySize, this change would be reflected to original array too. Check modified above example.

typedef unsigned char BYTE;

void checkArraySize(BYTE data[])
{
int internalSize = sizeof(data);
printf("%d\n", internalSize );
data[3]= 0x02; //internalSize is reported as 4
}

void main(void)
{
BYTE data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
int externalSize = sizeof(data); //externalSize is reported as 8
printf("Value before calling function: 0x%x\n",data[3]);
checkArraySize(data);
printf("Value after calling function: 0x%x\n",data[3]);
}

output would be:

Value before calling function: 0x4
4
Value after calling function: 0x2


Related Topics



Leave a reply



Submit