Arrays, pointers and functions question

Hello all,

Below I have some code that I'm having trouble with. I make an array called myArray and initialise it with the integers from 1 to 10. I output three things to start with , myArray, &myArray and &myArray[0]. All of them appear to have the same memory address. My first question is: why does this happen?

When I pass either myArray or &myArray[0] to print_array, it will output as expected, when I pass &myArray, it will not compile - if somebody could explain to me why this happens, I'd be very grateful.

Thirdly, in my print_array function, I have hard-coded the size of my array. Is there a way that C++ can figure out how large the array is?

I'm quite willing to believe there is a much neater way of passing an array to a function by reference, if anyone could help me find out what it is, I'd be very happy!

Many thanks,
Len.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

void print_array( int* );

int main()
{
	int myArray[] = {1,2,3,4,5,6,7,8,9,10};
	cout << "myArray = " << myArray << endl;
	cout << "&myArray = " << &myArray << endl;
	cout << "&myArray[0] = " << &myArray[0] << endl << endl;
	
	print_array(myArray);
	return 0;
}

void print_array( int* array )
{
	for(int i = 0; i < 10; i++)
	{
		cout << array[i] << " ";
	}
	cout << endl;
}


Output:
1
2
3
4
5
myArray = 0x7fff5fbfec60
&myArray = 0x7fff5fbfec60
&myArray[0] = 0x7fff5fbfec60

1 2 3 4 5 6 7 8 9 10 


&myArray, it will not compile - if somebody could explain to me why this happens, I'd be very grateful.

&myArray will be an int**, you can't pass that to a function that expects an int*.


Is there a way that C++ can figure out how large the array is?

Not with those array types. You normally do that by passing the size of the array along with the function.


I'm quite willing to believe there is a much neater way of passing an array to a function by reference, if anyone could help me find out what it is, I'd be very happy!


Yes, it's called "Arrays are evil, use std::vectors instead". Your program with vectors:

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
#include <iostream>
#include <vector>
using namespace std;

void print_array( vector<int>& );

int main()
{
	vector<int> myVector;
        for(int i=0; i<10; ++i)
        {
              myVector.push_back(i+1);
         }
	
	print_array(myVector);
	return 0;
}

void print_array( vector<int> &array )
{
	for(int i = 0; i < array.size(); i++)
	{
		cout << array[i] << " ";
	}
	cout << endl;
}
Last edited on
Dear hanst99,

Thanks for your quick reply! Are there any performance issues by using vectors instead of arrays?

I noticed that in your code your prototype for print_array says that you're going to give it a reference to a vector of ints, but actually, you just give it a vector - would it be possible to explain why this is the case? When I tried to pass it a reference to the vector, it still compiled.

i.e. this still works
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
#include <iostream>
#include <vector>
using namespace std;

void print_array( vector<int>& );

int main()
{
	vector<int> myVector;
        for(int i=0; i<10; ++i)
        {
              myVector.push_back(i+1);
         }
	vector<int>& rMyVector = myVector;
	print_array(rMyVector);
	return 0;
}

void print_array( vector<int> &array )
{
	for(int i = 0; i < array.size(); i++)
	{
		cout << array[i] << " ";
	}
	cout << endl;
}


EDIT: also, if I didn't pass a reference, but the actual vector, would it just pass a reference anyway, like it would with arrays?
Last edited on
I noticed that in your code your prototype for print_array says that you're going to give it a reference to a vector of ints, but actually, you just give it a vector - would it be possible to explain why this is the case?


Magic. print_array(myVector); You can think of it as "it creates a reference to myVector here and passes it".

lso, if I didn't pass a reference, but the actual vector, would it just pass a reference anyway, like it would with arrays?


Well, not the same as with arrays. In your example, you would simply pass the array pointer - arrays in C++ are implicitly pointers - around, whereas references behave more like as if they are the actual object.

For example:

1
2
3
4
5
6
7
8
9
A object1;
A object2;
A *ptrObject;
A &refObject;

ptrObject = &object1; //ptrObject points to object1
ptrObject = &object2; //ptrObject points to object2
refObject = object2;   //refObject becomes a reference to object2
refObject = object1; //is the same thing as object2 = object1 


References behave more like aliases, and once you have bound them to an object you can't seperate them anymore. Pointers are just numbers that save the location of something in memory.
Magic


Is this also known as casting?
No, it's not a cast. As I said earlier, it's best to think of a reference to an object as the same thing as the object, with the only difference being that it's not a distinct entity, but rather a sort of "place holder" for another object.
Ok I think I get it, thanks!

One last thing - does using vectors come at a performance price?
The STL classes (like vector, map etc) are very perfomant. Trust me, the little extra overhead they have is completely neglibible, especially compared to the functionality gain they provide. In C++, use STL containers over arrays whenever possible, only use arrays when you really have to.
Topic archived. No new replies allowed.