In this tutorial, we will learn how the pointers and arrays in C are related, difference between pointers and arrays.
Pointers and arrays are strongly related. Whenever an array name appears in an expression, it is automatically converted to a pointer to its first element.
In above example, a will refer to the address 2886728 . As &a references address so &a is equal to a.
Arithmetic manipulation to Pointer
if p is a pointer to some element of an array, then p++ increments p to point to the next element
float num, array;
float *p1, *p2, *p3 = &array;
int i=2, j;
p1 = NULL; /* Assignment to NULL */
p2 = # /* Assignment to an address. */
p1 = p2; /* Assignment to another pointer (of same type). */
p2 = p3 - 4; /* Addition or subtraction by an integer */
p2 += i; /* Another pointer-offset expression. */
j = p3 - p2; /* Pointer subtraction: gives the number of elements between p2 and p3. */
i = p2 < p3; /* Relational operations <, >, ==, !=, <=, >= */
The arithmetic and relational operations are only valid for pointers to elements of the same array.
//program to print sum of 2 numbers using pointer to an array
int i, x, sum = 0;
p = x;
printf("Enter the number:");
for( i = 0; i < 2; i++ )
scanf("%d",( p + i ));
sum += *(p+i);
printf("Sum = %d", sum);
Enter the numbers: 1
sum = 3
Difference between pointers and arrays in C programming