C

Examples Of Pointers in C Programming

A variable that stores the address of another variable is known as a pointer. In contrast to other variables that hold values of a specific type, a pointer variable holds the address of a variable. An integer variable, for example, holds (or stores) an integer value, whereas an integer pointer holds the address of an integer variable. With the help of examples, we will go over pointers in C programming.
Before we get into pointers in C, let’s look at a simple example to see what we mean by a variable’s address.
A simple example of how to access the address of a variable without using pointers?
In this program, we have an int variable called num. The value of num is ten, which must be stored somewhere in memory, right? Each variable has its own memory space that stores its value; this memory space has an address. For example, we live in a house with an address, which makes it easier for others to find us. Similarly, the variable’s value is stored in a memory address, which allows the C program to find that value when it is needed.
So, if the address assigned to variable num is 0x7fff5694dc58, whatever value we assign to num should be stored at this location: 0x7fff5694dc58. Please see the diagram below.

#include <stdio.h>
int main()
{
   int num = 10;
   printf("Value of variable num is: %d", num);
   /* To print the address of a variable we use %p
    * format specifier and ampersand (&) sign just
    * before the variable name like &num.
    */
   printf("\nAddress of variable num is: %p", &num);
   return 0;
}

Output:

Value of variable num is: 10
Address of variable num is: 0x7fff5694dc58

A Simple Pointer Example in C

This program demonstrates how to declare and use a pointer. There are several other things we can do with pointers, which we will cover later in this guide. For the time being, all we need to know is how to connect a pointer to the address of a variable.
The data type of the pointer and the variable must match; an int pointer can hold the address of an int variable, and a pointer declared with a float data type can hold the address of a float variable. The pointer and variable in the following example are both of the int type.

#include <stdio.h>
int main()
{
   //Variable declaration
   int num = 10;

   //Pointer declaration
   int *p;

   //Assigning address of num to the pointer p
   p = #

   printf("Address of variable num is: %p", p);
   return 0;
}

Output:

Address of variable num is: 0x7fff5694dc58

C Pointers are operators that work with Pointers.
Let’s look at how the operators & and * are used with Pointers in C.

Operator “Address of”(&)

We already saw in the first example how the ampersand sign can be used to display the address of a variable. To get the address of variable num, I used &num. The & operator is also referred to as the “Address of” Operator.

printf("Address of var is: %p", &num);

It is worth noting that %p is a format specifier that is used to display the address in hex format.
Now that you know how to get a variable’s address, how do you store that address in another variable? This is where pointers come into play. As stated at the outset of this guide, pointers are used in C programming to hold the address of other variables.
A pointer is similar to another variable in that it stores the address of another variable rather than its value.

“At Address Value” (*) Operator

The * operator is also referred to as the Value at address operator.
How should a pointer be declared?

int *p1  /*Pointer to an integer variable*/
double *p2  /*Pointer to a variable of data type double*/
char *p3   /*Pointer to a character variable*/
float *p4   /*pointer to a float variable*/

The preceding are a few pointer declaration examples. If you need a pointer to store the address of an integer variable, the pointer’s data type should be int. The same is true for the other data types.
We can access the value of a variable via a pointer by using the * operator.
As an example:

double a = 10;
double *p;
p = &a;

*p returns the value of the variable a. The output of the following statement would be 10.

printf("%d", *p);

Similarly, assigning a value to *pointer would change the value of variable a. The preceding statement raises the value of a from 10 to 200.

*p = 200;

Pointer example demonstrating the use of & and *

#include <stdio.h>
int main()
{
   /* Pointer of integer type, this can hold the
    * address of a integer type variable.
    */
   int *p;

   int var = 10;

   /* Assigning the address of variable var to the pointer
    * p. The p can hold the address of var because var is
    * an integer type variable.
    */
   p= &var;

   printf("Value of variable var is: %d", var);
   printf("\nValue of variable var is: %d", *p);
   printf("\nAddress of variable var is: %p", &var);
   printf("\nAddress of variable var is: %p", p);
   printf("\nAddress of pointer p is: %p", &p);
   return 0;
}

Output:

Value of variable var is: 10
Value of variable var is: 10
Address of variable var is: 0x7fff5ed98c4c
Address of variable var is: 0x7fff5ed98c4c
Address of pointer p is: 0x7fff5ed98c50

Let’s look at some more examples to help you understand it better:
Assume we have a char variable ch and a pointer ptr that contains ch’s address.

char ch='a';
char *ptr;

Determine the value of ch.

printf("Value of ch: %c", ch);
or
printf("Value of ch: %c", *ptr);

Alter the value of ch

ch = 'b';
or
*ptr = 'b';

The preceding code would change the value ‘a’ to ‘b’.

Can you predict the outcome of the following C program?

#include <stdio.h>
int main()
{
    int var =10;
    int *p;
    p= &var;

    printf ( "Address of var is: %p", &var);
    printf ( "\nAddress of var is: %p", p);

    printf ( "\nValue of var is: %d", var);
    printf ( "\nValue of var is: %d", *p);
    printf ( "\nValue of var is: %d", *( &var));

    /* Note I have used %p for p's value as it represents an address*/
    printf( "\nValue of pointer p is: %p", p);
    printf ( "\nAddress of pointer p is: %p", &p);

    return 0;
}

Output:

Address of var is: 0x7fff5d027c58
Address of var is: 0x7fff5d027c58
Value of var is: 10
Value of var is: 10
Value of var is: 10
Value of pointer p is: 0x7fff5d027c58
Address of pointer p is: 0x7fff5d027c50

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button