Pointers Vs Arrays in C Programming Language

Pointers vs Arrays in C Programming Language

Pointers vs Arrays in C programming Language is very fundamental concept.The understanding of pointers vs arrays in C programming language is regarded as the most complex problems .For the clear understanding, I have started this tutorials with some examples.Let’s begin with pointers vs arrays in C programming language with simple examples:

A simple example of pointers vs arrays in C programming language

[c] void main()
{
int i = 4, *x ;
float j = 1.5, *y ;
char k = ‘c’, *z;

printf (“\nValue of i = %d”, i) ;
printf (“\n Value of j = %f”,j);
printf (“\nValue of k = %c”, k);
x = &i;
y = &j;
z = &k;
printf (“\nOriginal address in x = %u”, x);
printf (“\nOriginal address in y = %u”, y);
printf (“\nOriginal address in z = %u”, z);
x++;
y++;
z++;
printf (“\nNew address in x = %u”, x);
printf (“\nNew address in y = %u”, y);
printf ( “\nNew address in z = %u”, z);
}
[/c]

Suppose i, j and k are stored in memory at addresses 1000, 2000 and 5000, the output would be…
Value of i = 4
Value of j = 1.5
Value of k = c
Original address in x = 1000
Original address in y = 2000
Original address in z = 5000
New address in x = 1002
New address in y = 2004
New address in z = 5001

Observe the last three lines of the output. 1002 is original value in x plus 2, 2004 is original value in y plus 4, and 5001 is original value in z plus 1. This so happens because every time a pointer is incremented it points to the immediately next location of its type That is why, when the integer pointer x is incremented, it points to an address two locations after the current location, since an int is always 2 bytes long. Similarly, y points to an address 4 locations after the current location and z points 1 location after the current location. This is a very important result and can be effectively used while passing the entire array to a function.

The way a pointer can be incremented, it can be decremented as well, to point to earlier locations. Thus, the following operations can be performed on a pointer:

  • Addition of a number to a pointer. For example
[c] int i = 4, *j, *k;
j = &i;
j=j + 2;
j=j + 6;
k = j + 3;
[/c]
  • Subtraction of a number from a pointer. For example
[c] int i = 4, *j, *k ;
j = &i;
i=j-1.;
j=j-4;
k=j-7;
[/c]
  • Subtraction of one pointer from another.

One pointer variable can be subtracted from another provided both variables point to elements of the same array. The resulting value indicates the number of bytes separating the corresponding array elements. This is illustrated in the following program.

[c] void main()
{
int a[ ] = { 1, 2, 3, 4, 6, 5, 7};
int *i, *j;
i = &a[1];
j = &a[5];
printf(“%d%d”,j-i,*j-*i);
}
[/c]

Here i and j have been declared as integer pointers holding addresses of first and fifth element of the array respectively.
Suppose the array begins at location 4002, then the elements a[1] and a[5] would be present at locations 4004 and 4012 respectively, since each integer in the array occupies two bytes in memory.

  • Comparison of two pointer variables

Pointer variables can be compared provided both variables point to objects of the same data type. Such comparisons can be useful when both pointer variables point to elements of the same array.The comparison can test for either equality or inequality. Moreover, a pointer variable can be compared with zero (usually expressed as NULL). The following program illustrates how the comparison is carried out.


[c] void main()
{
int a[ ] = { 1, 2, 3, 4, 6, 5, 7};
int *j, *k;
j = &a [ 4 ];
k = ( a + 4);
if(j==k)
printf (“The two pointers point to the same location”); .
else
printf (“The two pointers do not point to the same location”) ;
}
[/c]

A word of caution! Do not attempt the following operations on pointers… they would never work out.
(a) Addition of two pointers
(b) Multiplication of a pointer with a constant
(c) Division of a pointer with a constant
Now we will try to correlate the following two facts, which we have learnt above:
(a) Array elements are always stored in contiguous memory locations.
(b) A pointer when incremented always points to an immediately next location of its type.

Suppose we have an array

 n[ ] = { 25, 33, 12, 44, 56, 17 }.

The following figure shows how this array is located in memory. Here is a program that prints out the memory locations in which the elements of this array are stored.

Pointers vs Arrays in C Programming Language
Pointers vs Arrays in C Programming Language

[c] main()
{
int n[] = {25, 33,12, 44, 56,17};
int i;
for (i = 0 ; i <= 5 ; i++)
{
printf (“\nelement no. %d\t “, i);
printf (“address = %u\n”, &n[i]);
}
}

[/c]

Pointers vs Arrays in C Programming Language

The output of this program would look like this:
element no. 0 address = 4000
element no. 1 address = 4002
element no. 2 address = 4004
element no. 3 address = 4006
element no. 4 address = 4008
element no. 5 address = 4010

Note that the array elements are stored in contiguous memory locations, each element occupying two bytes, since it is an integer array. When you run this program, you may get different addresses, but what is certain is that each subsequent address would be 2 bytes.
Our next two programs show ways in which we can access the elements of this array.

[c] main()
{
int n[] = {25, 33,12,44, 56,17};
int I ;
for (i = 0 ; i <= 5 ; i++)
{
printf (“\naddress = %u\t “, &n[i]);
printf (“element = %d\n”, n[i]);
}
}
[/c]

Pointers vs Arrays in C Programming Language

The output of this program would be:
address = 4000 element = 25
address = 4002 element = 33
address = 4004 element = 12
address = 4006 element = 44
address = 4008 element = 56
address = 4010 element = 17
We will study the next method, which accesses the array elements using pointers.so,Pointers vs Arrays in C Programming Language are associated with each others. A good example of pointers vs arrays in C programming language.

[c] void main()
{
int n[ ] = {25, 33,12, 44, 56,17};
int i, *j;
j = &n[0]; /* assign address of zeroth element */
for (i = 0 ; i <= 5 ; i++)
{
printf (“address = %u\t”, j);
printf (“element = %d\n”,*j);
j++ ; /* increment pointer to point to next location */
}
}
[/c]

Pointers vs Arrays in C Programming Language

The output of this program would be:
address = 4001 element = 25
address = 4003 element = 33
address = 4005 element = 12
address = 4007 element = 44
address = 4009 element = 56
address = 4011 element = 17
In this program, to begin with we have collected the base address of the array (address of the 0th element) in the variable j using the statement,

When we are inside the loop for the first time, j contains the address 4000, and the value at this address is 25. These are printed using the statements,

	printf ("address = %u\t", j); 
	printf ("element = %d\n", *j);

On incrementing j it points to the next memory location of its type (that is location no. 4002). But location no. 4002 contains the second element of the array, therefore when the printf( ) statements are executed for the second time they print out the second element of the array and its address (i.e. 33 and 4002)… and so on till the last element of the array has been printed.A good example of pointers vs arrays in C programming language.

For the books, I would like to recomend these books which are greatful resoucrces for the students

Pointers and Arrays Resouces

Pointers and Arrays Resouces

So,it is very important to understand the pointers vs arrays in c programming language.If you do not understand the concept, then it is hard to further understand the pointer vs arrays in c programming language.

This entry was posted in Uncategorized. Bookmark the permalink.