Passing Arrays by Reference

Does that work? I would like to run some code in the main, then it jumps to a void, where an array is given its ints, then it should return to the int main () where it continues using the array with the ints from the void.
yaa it will work...
suppose your this is your array
int arr[N];
you can call any function
like,
functionName(&arr);
hope, this will help you..
OK, but can you still give me an example, for it doesnt work in my program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

void doSomething (int* theArray)
{
  theArray[0] = 7;
  theArray[4] = 9;
}

int main()
{
  int arr[10];
  cout << arr[0] << " " << arr[4] << endl;
  doSomething(arr);
  cout << arr[0] << " " << arr[4] << endl;
}
Last edited on
There are two possibilities: either pass an array by reference or pass an array by value. When an array is passed by value it is converted to the pointer to its first element. I will show two variants.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#innclude <iostream>

void fill( int *a, int n )
{
   for ( int i = 0; i < n; i++ ) a[i] = i;
}

const int N = 10;

void reverse_fill( int ( &a )[N] )
{
   for ( int i = 0; i < N; i++ ) a[i] = N - i - 1;
}

int main()
{
   int a[N];

   fill( a, N );

   for ( int i = 0; i < N; i++ ) std::cout << a[i] << ' ';
   std::cout << std::endl;

   reverse_fill( a );

   for ( int i = 0; i < N; i++ ) std::cout << a[i] << ' ';
   std::cout << std::endl;

   return 0;
}


The difference between the functions is that in the second function that gets the array by reference the size of the array shall be a constant expression. It is why constant N was declared before the function declaration.
Last edited on
closed account (o1vk4iN6)
pass an array by value


Wouldn't it be better to say "array pointer" otherwise to me it sounds like a copy of the array is being made and any modifications won't effect the original.

@xerzi
Wouldn't it be better to say "array pointer" otherwise to me it sounds like a copy of the array is being made and any modifications won't effect the original.


You are not specifying a pointer as the argument. You are specifying the array which implicitly will be converted to the pointer to its first element.

And I do not know such term as "array pointer". Atl least there is no such a term in the C++ standard.
Last edited on
closed account (o1vk4iN6)
to say you are passing an array by value is poorly worded and easily confused by everyone not living in the subset of c++.
I do not know also what is "subset of C++". I know C++ and try to use terms that are defined in the C++ standard. I do not bother that somebody who does not live in "the subset of C++" can be confused . Every programming language has its own system of terminolodgy that every one should follow. Moreover this is a forum on C/C++.

And as I menitioned already above there is no such term as "array pointer" in the C++ standard. So it is such a term as "array pointer" that can confuse everybody who knows C++.
Last edited on
closed account (o1vk4iN6)
"pass array by value" is not C++ terminology, it is common amongst all programming languages and describes how an object is to be passed to a function. The only reason this would be meaningful is if you know specifically how c-arrays work, and even still when you say array I would be assuming std::array which would make your examples incorrect with your saying. Use proper terminology: "c-array".

So if you read in the standard there is a teapot orbiting the sun you would believe it, use some of your own brain power.
@xerzi
when you say array I would be assuming std::array



It is only your personal problem what you are assuming when somebody are speaking about arrays.:)

One more there is no such term as c-array in the C++ Standard. There is term array that denotes a compound type and std::array (I hope you see namespace name std) that denotes a standard template container.
Moreover this theme deals with the compound type array and nobody except you assumed that the author of the theme are speaking about the standard container std::array.

Moreover I'd like to point out that std::array appeared only in C++ 2011 so nobody was confused when somebody was saying about arrays. :)
closed account (o1vk4iN6)
nobody except you


As far as I can tell it's just me and you. So idk who you are talking about other then your imaginary peers.

If I were to say "string", which would you think I am talking about. Think about it, it is related. :)

As such again keep your mind open, the community makes their own distinctions to add clarification to things the standard does not. It is a community driven effort and if all you do is lock yourself in your room then continue.

appeared only in C++ 2011 so nobody (note: only you [vlad]) was confused


Then I suggest you read the new standard and get yourself up-to-date grandpa. :)
I am sorry I am not a doctor. It is maybe a doctor who will be interesting your problems.
Last edited on
closed account (o1vk4iN6)
I know you are not a physician (unless you mean doctor of computer science, then sorry to hear that), I am trying to help you. You probably don't know what it means to communicate a thought, with clarity, to another person so they can understand you, as you are clearly using google translator with that terrible grammar.
Topic archived. No new replies allowed.