1

I have one quick question about the passing of arrays in C++ which I don't understand.

Basically when you want to pass a array of type integer to another function you have to pass an address to that array instead of directly passing the whole block of contiguous memory. Exactly why is the case?

Also, why is that char arrays can directly be passed to another function in C++ without the need to pass an address instead??

I have tried looking for learning materials for this online (such as cplusplus.com) but I haven't managed to find and explanation for this.

Thanks for your time, Dan.

4
  • Your first assertion is not entirely correct, which renders the question hard to answer. You don't have to explicitly pass an address, because arrays decay into pointers when passed to functions that expect pointers. A char array is no different to any other type of array. But note that you can also pass references to arrays. Commented May 11, 2014 at 9:11
  • 'Exactly why is the case?' Beause array variables decay to pointers on the stack. Commented May 11, 2014 at 9:11
  • Do you mean getting your own copy by saying "passing the whole block of contiguous memory"? Commented May 11, 2014 at 9:20
  • 1. Use vectors. 2. It saves on time/memory. What is the point of copying the lot? Commented May 11, 2014 at 9:26

3 Answers 3

2

As long as C++ is concerned, passing char arrays and int arrays are same.

There are 2 ways to pass arrays in c++.

Address is passed

int fn(int *arrays, int len);
int fn(int arrays[], int len); // Similar to above, still staying as sytax hangover from anci c

Array reference is passed

int fn(int (&array)[SIZE]); // Actual array passed as reference

You can templatized above function as

template<size_t SIZE>
int fn(int (&array)[SIZE]);

Above method allows you to pass array of anysize to this function. But beware, a different function is created from template for each size. If your function's side effect changes a local state (static variable for ex), this should be used with care.

If you don't want to change contents, use const with arguments.

If you want a copy of array in function argument, consider using stl container like std::array or std::vector or embed array in your class.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow great answer, but for char arrays can't you just pass the whole char array into a function without a reference/pointer?
Can you give a code example. Either the address of first character (i.e. a pointer) is passed behind the scene, or you might be using std::string. (When you write strcpy(dst, "my_name"), you actually pass the address of memory location containing array "my_name")
@Supertecnoboff: In C it is not possible to have a function parameter be an array type. If you try to write a parameter of array type "array of X", it will be automatically changed to a pointer type "pointer to X".
2

It isn't entirely clear from your question exactly what you're trying and what problems you've had, but I'll try to give you useful answers anyway.

Firstly, what you're talking about is probably int[] or int* (or some other type), which isn't an array itself... its a pointer to a chunk of memory, which can be accessed as if it were an array. Because all you have is a pointer, the array has to be passed around by reference.

Secondly, passing around an array as a "whole block of contiguous memory" is rather inefficient... passing the point around might only involve moving a 32 or 64 bit value. Passing by reference is often a sensible thing with memory buffers, and you can explicitly use functions like memcpy to copy data if you needed to.

Thirdly, I don't understand what you mean about char arrays being "directly" passable, but other types of arrays cannot be. There's nothing magic about char arrays when it comes to passing or storing them... they're just arrays like any other. The principle difference is that compilers allow you to use string literals to create char arrays.

Lastly, if you're using C++11, you might want to consider the new std::array<T> class. It provides various handy facilities, including automatic memory management and keeping track of its own size. You can pass these by value, template<class T> void foo(std::array<T> bar) or by reference template<class T> void foo(std::array<T>& bar), as you like.

Comments

1

You can't pass any array by value. You can pass by value either a struct containing array or std::array from C++11.

3 Comments

Or you can pass an array by reference.
@juanchopanza it's wont pass "the block of contiguous memory" on stack.
True, you don't get your own copy, if that's what OP is after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.