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

I am trying to write a function that outputs elements in an array. However, when I work with transferred arrays, I do not know how to iterate over an array.

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

What is the best way to iterate over a passed array?

+7
c arrays
source share
6 answers

You also need to pass the size of the array to the function.
When you pass an array to your function, you really pass the address of the first element in that array. Thus, the pointer points only to the first element in your function.

Since the memory in the array is continuous, you can still use pointer arithmetic such as (b+1) to point to the second element or equivalent to 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 do not match with pointers .

+16
source share

Why don't you use function templates for this (C ++)?

 template<class T, int N> void f(T (&r)[N]){ } int main(){ int buf[10]; f(buf); } 

EDIT 2:

Now qn has a C tag and the C ++ tag has been removed.

+5
source share

For C, you need to pass the length (number of elements) of the array.

For C ++, you can pass the length, BUT, if you have access to C ++ 0x, BETTER use std::array . See here and here . It carries length and provides an access check if you access elements using the member function at() .

+2
source share

You can try this ...

 #include <cstdio> void print_array(int b[], size_t N) { for (int i = 0; i < N; ++i) printf("%d ", b[i]); printf("\n"); } template <size_t N> inline void print_array(int (&b)[N]) { // could have loop here, but inline forwarding to // single function eliminates code bloat... print_array(b, N); } int main() { int a[] = { 1, 2 }; int b[] = { }; int c[] = { 1, 2, 3, 4, 5 }; print_array(a); // print_array(b); print_array(c); } 

... interesting b does not work ...

 array_size.cc: In function `int main()': array_size.cc:19: error: no matching function for call to `print_array(int[0u])' 

JoshD points out in comments below arrays of size re 0 of size (GCC extension) and above that size.

0
source share

In C99, you can require an array to have at least n elements:

 void print_array(int b[static n]); 

6.7.5.3.7 : the declaration of the parameter as an `` array of type must be adjusted to a qualified pointer to a type in which the quality type (if any) is that specified in [and] the array type. If the static keyword also appears in [and] the type of the array, then for each function call the value of the corresponding actual argument should provide access to the first element of the array, at least as many elements defined by the size expression.

In GCC, you can pass the size of an array implicitly as follows:

 void print_array(int n, int b[n]); 
0
source share

In C ++, you can also use some type of list class, implemented as an array with a size method or as a structure with a size member (in c or C ++).

0
source share

All Articles