Topic :Pointer Vs Arrays in C++ Programming Language:



Pointer Vs Arrays:

Pointers ana Arrays are strongly related. In fact, pointer and Arrays are interchangeable in many cases. For Example: a pointer that points to the beginning of an array can access that array by using either pointer arithmetic or array-style indexing. Consider the Following program.



/* Pointer Vs Arrays: InfoBrother:*/
 
#include <iostream> 
using namespace std;
 
const int MAX = 3;
 
main () 
{
   int var[MAX] = {10, 100, 200};
   int *ptr; //pointer to int.
 
   // 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;
}


However, pointers and arrays are not completely interchangeable. For Example: Consider the following program:



#include <iostream>
using namespace std;
 
const int MAX = 3;
 
int main () 
{
	int var[MAX] = {10, 100, 200};
 
	for (int i = 0; i < MAX; i++) 
	{
		*var = i; // This is a correct syntax
		var++; // This is incorrect.
	}
 
	return 0;
}


It is perfectly acceptable to apply the pointer operator * to var but it is illegal to modify var value. The reason for this is that var is a constant that points to the beginning of an array and can not be used as value. because an array name generates a pointer constant, it can still be used in pointer-style expressions, as long as it is not modified. For example, the following is a valid statement that assigns var[2] the value 500.



*(var + 2) = 500;


Above statement is valid and will compile successfully because var is not changed.

Most of the time, pointer and array accesses can be treated as acting the same, the major exceptions being.


  • The Sizeof() Operator:

    » Sizeof(array): returns the amount of memory used by all elements in Array.
    » Sizeof(pointer): only returns the amount of memory used by the pointer variable itself.



  • The & Operator:

    » &array: It is an alias for &array[0] and returns the address of the first element in array.
    » &pointer: This pointer returns the address of pointer.



  • A string literal Initialization of a character array:

    » char array[] = "abc": Sets the first four elements in array to 'a', 'b', 'c' and '\0'.
    » char *pointer = "abc": Sets pointer to the address of the "abc" string ( which may be stored in read only memory and thus unchangeable).



  • Pointer variables can be assigned a value whereas array variable can't be:

    Pointer Variables can be assigned a value whereas array variable can't be, as shown in the given example:

    int a[10];
    int *p; 
    p=a; //legal
    a=p; //illegal


  • Arithmetic on pointer variable is allowed:

    Arithmetic on pointer variables is allowed in C++ Programming, as shown in the given example:

    int a[10];
    int *p; 
    p++; //legal
    a++; //illegal


Example:

Let's have an simple example to clear the concept what we discussed above.


/* Pointer Vs Arrays: InfoBrother.*/
 
#include <iostream>
using namespace std;
 
main()
{
	float arr[5]; //array
	float *ptr; //pointer
 
	cout << "Displaying address using arrays: " << endl;
	for (int i = 0; i < 5; ++i)
	{
	     cout << "&arr[" << i << "] = " << &arr[i] << endl;
	}
 
	// ptr = &arr[0]
	ptr = arr;
 
	cout<<"\nDisplaying address using pointers: "<< endl;
	for (int i = 0; i < 5; ++i)
	{
	     cout << "ptr + " << i << " = "<< ptr + i << endl;
	}
 
	return 0;
}







I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement