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



Pointer and Arrays:


It is Most Likely that you would not understand this Tutorial Until you Go through the Tutorial related C++ Pointers so it would be better for you to first take C++ Pointers Tutorial then come back to this tutorial. so you can understand about the relationship between Pointer and Arrays:


In C++, there is a close relationship between pointer and Arrays, In fact, frequently a pointer and an Array are interchangeable. The Identifier of an Array is equivalent to the address of its first element, like a pointer is equivalent to the address of the first element that it points to, to infect they are the same thing.



Explanation:

An Array is just like an City block. Each Element of the Array corresponds to a house on that block. Here, However, the Array elements are Measured by the Number of houses from the beginning of the block (The street corner). Say that the house right on the corner is 220 Main Street, which means that the house one house from the corner is 221 Main Street, and so on. Using Array terminology, we would say cityBlock[ 0 ] is 220 Main Street, cityBlock[ 1 ] is 221 Main Street, and so on.

Take that Same Model back to the world of Computer Memory. Consider the case of an Array of 32 1-byte Character called Name. if the first byte of this Array is stored at address 0x110, the Array will extend over the range 0x110 through 0x12f. Name[ 0 ] is located at address 0x110, Name[ 1 ] is at 0x111, Name[ 2 ] at 0x112, and so on.

Take this Model one step further to the world of pointer variables. After executing the Expression.


ptr    &Name [ 0 ];


Example:

Let's have an simple Example.



double *ptr ;
double balance[ 10 ] ;
ptr = balance ;


Balance is a pointer to &balance[0] which is the address of the first element of the Array balance. Thus, the following program fragment Assigns ptr the address of the first Element of balance.

It is Legal to use Array Names as constant pointers, and vice versa. Therefore, *( balance + 4 ) is a legitimate way of accessing the data at balance[ 4 ]. Once we store the address of first element in ptr, we can access Array elements using *ptr,   *( ptr + 1 ),   *( ptr + 2 ) and so on. Below is the example to show all the concepts discussed above:



/* Array and Pointer- InfoBrother*/
 
#include <iostream>
using namespace std;
main()
{
 
	int balance[5] = {10, 20, 30, 40, 50}; // an array with 5 elements.
	int *ptr; //declaring an pointer.
	ptr = balance; //assign first element of Array to ptr: (ptr = balance[0]);
 
 
	// output each array element's value
	cout << "Array values using pointer " << endl;
	for ( int i = 0; i < 5; i++ )
	{
		cout << "*(ptr + " << i << ") : ";
		cout << *(ptr + i) << endl;
	}
 
	cout << "\n\n Array values using balance as address " << endl;
	for ( int i = 0; i < 5; i++ )
	{
		cout << "*(balance + " << i << ") : ";
		cout << *(balance + i) << endl;
	}
 
	return 0;
}


In the above example, Ptr is a pointer to int which means it can store address of a variable of int type. Once we have address in ptr then *ptr will give us value available at the address stored in ptr, as we have shown in the above example:













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