Difference between using pointers for an array and a single data point?
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the next location
ptr++;
}
return 0;
}
I don't understand why this code should work as the correct syntax
ptr=&var[0];
instead
ptr=var;
if var would be something like
int var=49;
then my version of the reasoning is executed and the previous program fails. Is their any separate concept of an array pointer and a single data point?
First, we need to understand that pointers and arrays are treated the same in C or C ++, at least once we got down to the definition. Let us understand a few things. In the example above, the following are equivalents (after ptr = var).
var [0], * var, ptr [0], ptr. Everyone == 10.
When:
ptr = &var[0];
This assigns the address of the first var element. Another way of saying & var is a pointer to a pointer to int. We know var is a pointer to int (int *) and * var is int.
If * var and var [0] are equivalent, then we make the equivalent of * (& var), which is just var.
Perhaps to make it clearer, you could think of a different index than 0. var [1] is the same as (var + 1). Then the following is done:
ptr = var + 1
coincides with
ptr = &var[1]
They are just two different ways of expressing the same thing.
In your example int var = 49 var is an int, not a pointer to int (remember that an array is just a pointer, so a pointer to an int is the same as an ints array). You can express it like:
int var=49;
ptr = &var;
Then if you remove the loop (or set MAX = 1), you get the result you are looking for.
Think about how to remove the addressing layer (pointers) and how to add it. This is a little confusing because * does the opposite when you define them.
int **i;
Points to a pointer to int. Then:
& i is pointer to pointer to pointer int. i is a pointer to an int pointer. * i - pointer to int ** i - int i [0] - pointer to int * i [0] - int
Pointers in C and C ++ are just a different data type. The question "Is their any separate concept of an array pointer and a single data point?" is a little flawed because in your example, you have an array (which is equivalent to a pointer), not an array to a pointer. The single data point (in the second example int var = 49) still has an address, but is not an array, so it is not a pointer type, it is an integer type. You could easily describe it as:
int var[1] = {49};
What is an array (pointer) to one int that should not be confused with int.
I don't understand why this code should work as the correct syntax
ptr=&var[0];
instead
ptr=var;
The two ways to assign a pointer are equivalent. In expressions that assign an array variable to a pointer, C ++ interprets the array name as a pointer to the original array element, i.e. &var[0]
...
If var would be something like
int var=49;
then my version of the reasoning persists and the previous program fails.
This is because C ++ provides implicit pointer conversion from arrays, but not from scalar variables.
The array name contains the address of its first element in memory. ie pointer.
int array[2] = {1,2}; // "array" is a pointer to array[0]
int **p = &array; //pointer to pointer to array[0]
The array name is reference
for the first index of the array. The correct way to assign an array is pointer
:
int *ptr = arrayName;
OR
int *ptr = &arrayName[0];
Another thing to keep in mind is that you can add an offset to the pointer to reference other elements in the array. This will point to the first aray *ptr++;
OR value *ptr+=1;
. Whereas doing it with a variable pointer will lead to unexpected results.