In following C program the array elements are accessed using pointer. Pointer stores base address of an array. Hence in this program ptr is a pointer to array x.
#include< stdio.h>
main()
{
int x[ ]={10,20,30,40,50};
int *ptr,i;
ptr=x; //ptr store base address of array
for(i=0;i<5;i++,ptr++)
{
printf(“\n address = %u”, &x[i]);
printf(“elements=%d %d %d %d”, x[i],*(x+i), i[x], *ptr);
}
}
In ‘C’ language all arrays are implemented as pointers. Hence x[i] => *(x+i) => *(i+x) => i[x] all are equivalent statement.
Also Read
- Static and Dynamic Memory Allocation
- Memory Leak and Dangling Pointer
- Memory Allocation for 2D Array
- Dynamic Memory Allocation
- Pointer Constant and Constant Pointer
- Pointer Declarations and their Meanings
- Functions and Pointers
- Initializing Pointer to Pointer
- Pointer to Pointer Multiple Indirection
- Relationship between Array and Pointer
- Pointer Arithmetic
- Types of Pointers
- Illustrate Basic Pointer Use
- Using Pointer
- Intializing Pointer
- Declaring Pointer
- Application of Pointers
- Value Model VS Reference Model
- What is Pointer
Comments
Post a Comment
Please give us feedback through comments