Pointer Handle Array Values in C


Pointers can be used to handle array values in C programming. When an array is declared, a memory block is allocated to store the values of the array. The memory address of the first element of the array is also stored. This address is also known as the base address of the array.

A pointer variable can be used to store the base address of the array. Using pointer arithmetic, we can move through the memory locations of the array and access the values stored at those locations.

Here is an example of how pointers can be used to handle array values in C:

  • The program starts by including the standard input-output library, stdio.h , which is used to input and output data.
  • In the main function, an integer array a is declared and initialized with the values 10, 20, 30, 40, and 50 . A pointer variable p is also declared.
  • The pointer variable p is then assigned the memory address of the first element of the array a using the address-of operator & with the name of the array.
  • The program then uses the sizeof operator to find the size of the integer variable and the array a in bytes. Also, the program uses sizeof(a)/sizeof(int) to find the length of the array.
  • The program also uses the printf function to print the value stored in p and the address of a
  • Then the pointer variable p is incremented using the p++ operator. This moves the pointer to the next memory location, which is the second element of the array.
  • The program again uses the printf function to print the value stored in p and the value stored at the memory location pointed to by p , which is the second element of the array.
  • The program then uses the printf function to print the values stored at the memory location pointed to by p after incrementing it using ++p and *++p

This program demonstrates how to use pointers to handle array values in C. It also demonstrates how to use the sizeof operator to find the size of a variable or array in bytes and how to use the printf function to output the values stored in a pointer variable and the memory location it points to.

Source Code

#include<stdio.h>
int main()
{
    int a[]={10,20,30,40,50};
    int *p;
    //{10,      20,     30,     40,     50}
    //{6356712,6356716,6356720,6356724,6356728}
    p=&a;
 
 
    printf("\nSize of integer    : %d",sizeof(int)); //4
    printf("\nSize of a          : %d",sizeof(a));  //20
    printf("\nLength of a        : %d",sizeof(a)/sizeof(int));//5
    printf("\nAddress of A       : %d",&a);//6356712
    printf("\nValue  of P        : %d",p);//6356712
 
    p++;
    printf("\nValue  of P        : %d",p);//6356716
    printf("\nValue  of *P       : %d",*p);//20
 
    printf("\nValue  of *++P        : %d",*++p);//30
    printf("\nValue  of ++*P        : %d",++*p);//31
 
    return 0;
}
 
 
To download raw file Click Here

Output

Size of integer    : 4
Size of a          : 20
Length of a        : 5
Address of A       : 6356712
Value  of P        : 6356712
Value  of P        : 6356716
Value  of *P       : 20
Value  of *++P        : 30
Value  of ++*P        : 31

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C