Single Pointers in C: A Step-by-Step Guide to Declaring, Initializing, and Using Pointers


A pointer in C is a variable that holds the memory address of another variable. A single pointer, also known as a "regular" pointer, can hold the memory address of a single variable.

The basic syntax for declaring a pointer in C is as follows:

           data_type *pointer_name;

Here, the data_type specifies the data type of the variable the pointer will be pointing to, and the pointer_name is the name of the pointer. The * operator is used to indicate that the variable is a pointer.

Here is an example of a single pointer being used in a program:

  • 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 variable a is declared and initialized with the value of 10. A pointer variable p is also declared.
  • The pointer variable p is then assigned the memory address of the variable a using the address-of operator &. In this way, p points to the memory location where the value of a is stored.
  • The program then uses the printf function to print the value of a, the memory address of a, the value stored in p (which is the memory address of a) and the memory address of p.
  • The program uses the dereference operator * to print the value stored at the memory address stored in p, which is the value of a.

This program demonstrates how to declare and initialize a single pointer, how to set it to the memory address of a variable, how to print the value stored at a memory address and how to dereference a pointer variable.

Source Code

#include<stdio.h>
int main()
{
    int a=10,*p;
    p=&a; //Address of a
 
    printf("\n Value of  A           : %d",a);
    printf("\n Address of  A         : %d",&a);
    printf("\n Value of  P           : %d",p);
    printf("\n Address of  P         : %d",&p);
    printf("\n P Dereferencing       : %d",*p);
 
    return 0;
}
 
To download raw file Click Here

Output

 Value of  A           : 10
 Address of  A         : 6356732
 Value of  P           : 6356732
 Address of  P         : 6356728
 P Dereferencing       : 10

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