DEV Community

kowshik996
kowshik996

Posted on • Updated on

Difference between pointers and arrays.

The difference between pointers and arrays

I have seen in many places that an array is introduced as a pointer. This is technically not correct. Arrays are not pointer. So what is it? It is like any other variable in C++.
Take a look at this code:

int arr[3]={3,4,5};
count<<arr;

You may say: "Look there, arr is an address, who says it's not a pointer?"
And I say: this code prints out an address. So var is an address?

int var;
count<<&var;

Let me explain:
All the variables can be manipulated in the memory by their addresses. CPU uses these addresses to fetch them, change them and save them. So, all variables have an address (not only those that you pointed to them). We can find out the address of a variable by (&) operator, in form of:

int* ptr=&intvar;
// Now the address of intvar is in ptr

An array is just a sequence of variables. But there is a rule, that C++ looks arrays as if they were pointers. What does it means? It means that if you write arr, compiler takes it as &arr0, that is the address of the first element of the array. It has a type "pointer to T" where T is the type of array elements (that is int in our example). If you add 1 to it it will point to the second element of the array.

So it takes &arr as what?
It is one of the exceptions where the rule does not apply. It takes it as a pointer to array. It will again point to the first element of the array, but if you add one to this pointer, it will point to the address of the place right after the last element of the array (just like you skipped the array). It is a pointer to the whole the array. The value of &arr and arr is the same (address of the first element). But their type is different. Here &arr has type "pointer to the array of T" (compare it to the type of arr).

Look at this

int arr[3]={3,4,5};
count<<"First element of the array: "<<arr[0] <<end l;
count<<"Address of the first element: "<<&arr[0] <<end l;
count<<"Address of the array: "<<arr <<end l;
count<<"So what is this? "<<&arr <<end l;

First count: Prints out the "value" of arr[0].
Second count: Prints out the "address" of arr[0].
Third count: Again prints out the "address" of arr[0].
Forth count: Prints out the address of the array, that is again the address of arr[0].

And this:

1
2
3
4
5
int arr[3]={3,4,5};
count<<"First element of the array: "<<arr[0]+1 <<end l;
count<<"Address of the first element: "<<&arr[0]+1 <<end l;
count<<"Address of the array: "<<arr +1<<end l;
count<<"So what is this? "<<&arr +1<<end l;

First count: Prints out the "value" of arr[0] plus one.
Second count: Prints out the "address"count of arr[1].
Third count: Again prints out the "address" of arr[1] .
Forth count: Prints out the first address of memory after the array.

Comparison
Similarities, each with an example:
1) (*) can be used for both.

int arr[3]={1,3,4}; //Declares an array with 3 elements
int * ptr=arr; //Initialize pointer ptr with the address of array arr
count<<(arr+2)<<endl;
count<<
{ptr+2)<<endl;

/* output */

4

2) Subscription can be used for both

int arr[3]={1,3,4}; //Declares an array with 3 elements
int * ptr=arr; //Initialize pointer ptr with the address of array arr
count<<arr[2])<<end l;
count<<ptr[2])<<end l;

/* output */
4
4

3) A pointer can be used as an array.

int *ptr=new int[3];
ptr[0]=12;
ptr[2]=3;
count<<ptr[2];

/* output */
3

4) An array can have a type of pointer. That means the elements of it can be pointers.

int ar[2]={8,2};
int variable 1=66;
int variable 2=111;
int* pt array[5];
pt array[0]=ar;
pt array[1]=&ar[1]; //Address of second element of array ar
pt array[2]=&var 1;
pt array[3]=&var 2;
pt array[4]=&ar[0];
// To keep code small I use a loop
for(int i=0;i<5;i++)
count<)<<end 1;

/* output */
8
2
66
111
8

5) All arrays will be passed to functions as a pointer, which means you can't actually send an array to a function. So function(char[]) is equal to function(char*).

Differences:

1) A pointer is a place in memory that keeps address of another place inside, while an array is a single, preallocated chunk of contiguous elements (all of the same type), fixed in size and location.

3) Array like pointers can't be initialized at definition like arrays.

1
2
char car[3]={'a','b',66};
char* cpt=new char[3]; //No way to be initialized here.

4) When we allocate memory for a pointer to use it as a dynamic array. The memory can be resized or freed later. But this is not the case for arrays.

For example:

1
2
3
char* pta=new char[12];
//Using pta
delete[] pta;

3) They produce different assembly code. Look and compare:
int main()
{
char arr[3];
char* ptr=new char[3];

arr[0]='C';   //Assembly is for this.  
ptr[0]='p';   //And for this.

return 0;
Enter fullscreen mode Exit fullscreen mode

}

Top comments (0)