Pointers and Arrays are the same things!
This statement might confuse you. How is this possible that pointers and arrays are the same? They never look to be same!
This post will clear this point to you. Let’s get started with the code below.
int array[5] = { 3, 4, 7, 8, 12 };
//Look! We have not put square brackets
//with a number after the name of the array
cout << array << endl;
What will it print? Will it print the first member of the array? No, it will print the memory address of the first member of the array.
Now, you see that there is no ampersand(&) needed for getting the memory address of the array. Don’t you think that it is something that is similar to pointers? Yeah, but this is not all!
int array[5] = { 3, 4, 7, 8, 12 };
//'pArray' is now pointed to the first member of the 'array'
int* pArray = array;
//Now, we can use 'pArray' similar to that of 'array'
cout << pArray[2] << endl; //Prints 7; same as 'array[2]'
The above code explains that pointers are actually arrays! We can have square brackets with pointers and just use it exactly like an array.
Another point to tell you that when we use a pair of empty square brackets([]) after a pointer or an array, it will still give us the memory address of the array. But when we mention the number in it, pointer or array will give us that particular value under the index.
Pointers and arrays are the same things but there is only one difference between them.
An array knows how many elements it has. If we use sizeof()
method for an array, we will get the right number of bytes that it occupies.
int array[5] = { 3, 4, 7, 8, 12 };
//We can use empty square brackets as well
cout << sizeof(array[]) << endl; //Prints (sizeof(int) * 5)
Note: Size of an integer may be different for your device. Mine is 4 so, it will result in 20. It may result in 10 as well.
A pointer never knows how many elements it has. It may or may not be an array as well. If we use sizeof()
with the pointer, we will not get the right number of bytes that it occupies. A pointer does know about the size of type that it is off.
int array[5] = { 3, 4, 7, 8, 12 };
int* pArray = array;
cout << sizeof(pArray[]) << endl; //Prints 2, 4 or 8 (not true in any case)
What if we want to pass an array to a function? We have never discussed that topic before. Now, there is something that I have to tell! We can’t pass an array to a function! So, there you go. The topic ends.
Of course, there is a solution to this problem. We can’t pass arrays but we can pass pointers to a function. Unfortunately, we can’t get the size of an array that is passed as a pointer. Therefore, we have to pass the total number of elements to the function as well.
void printIntegerArray(int* pArr, int totalElements)
{
for(int i = 0; i < totalElements; i++)
{
cout << pArr[i] << endl;
}
}
int main()
{
int array[5] = { 3, 4, 7, 8, 12 };
//We don't need to make any pointer
//Pointer is just a memory address
//So, we can pass array directly
printIntegerArray(array, 5);
return 0;
}
Just keep in mind, the concept that pointers are arrays but they don’t keep track of their number of elements. There are a few things that are not clear. We still don’t know what happens behind that pair of square brackets with a number in it. We will discuss pointer arithmetic in the next post along with the working behind square brackets.
Exercise: Make a function that takes an integer array and prints only even numbers.
One response to “C++ Programming Series: Pointers (Part 3)”
[…] arrays can be used with functions in exactly the same way as I have told in this post. Just keep in mind that we have to pass the size of the array into the […]