How To Use Pointer In C Programming?

Write a c programming to create, initialize and how to use of pointers. How to use address and access values using pointer variable in C programming language.

Knowledge Needed

Basic C programming and some basic concept of Pointers.

Basics Of Pointer

In C programming language the most important part is a pointer. So click here to learn what is pointers in C programming?

Pointer is a variable who stores the value of another variable. The important part of pointers are uses in dynamic memory allocations. That’s why c programming mostly called as lower level programming language.

Inside memory data stores in bytes form. Pointer is a tool to handle the lower level memory management. So, to access each bytes through address. The address ranges starts from zero(0) to some positive integers. By using pointer it handles lower level memory efficiently.

So now one question arises in mind, how to declare the pointer variables like other variables are declare? When declaring pointer variables is same as the normal variable declaration. Means if we declare,

int a;

the normal variable declaration then the pointer is also declare in similar way like,

int *p;

But one thing we have to notice that the normal variable and pointer variable both has similar type not the different type. Means if a variable declare a int type then the pointer variable access that variable data, the pointer should be int type not different type. Means

int a=2, *p;
p = &a;

So now we start to write the code how to use pointer and how it works on different programming concepts like array, function and dynamic memory allocation.

Reading Memory Address Of Any Variable

When we declare a variable it has name, data and memory address. Name is a identifier and data here is a constant which store in a memory location while the name is declared and assign the address on it.

For Example:

int num = 10;

Here num is a variable name and the value or data is 10. When num allocate the memory addresses are assigned on it.

To represent the address in programming language we are use & called unary operator. Here & uses as address operator.

Example: Get Memory Address Using Address Operator (&)

/**
 * C program to get memory address using address of operator
 */

#include <stdio.h>

int main()
{
    /* Simple Declarations */
    char character = 'Z';
    int integer = 175;
    float real = 100.40f;
    long long biginteger = 786458578ll;

    /* Print variable value with their memory address */
    printf("Value of character = %c, Address of character = %u\n", character, &character);
    printf("Value of integer = %d, Address of integer = %u\n", integer, &integer);
    printf("Value of real = %f, Address of real = %u\n", real, &real);
    printf("Value of biginteger = %lld, Address of biginteger = %u", biginteger, &biginteger);

    return 0;
}

So, after run this code the output will be

Output:
------
Value of character = Z, Address of chracter = 67356437
Value of integer = 175, Address of integer = 76367463
Value of real = 100.40f, Address of real = 63464536
Value of biginteger = 786458578ll, Address of biginteger = 78347847

Here we are using %u as representation of addresses, but we can use “%p” also used as format specifier for address in hexadecimal form not integer form.

Generally %u used for unsigned int. But here we are using to represent the addresses. While compiling the above code it shows some warning messages.

Example: To Create, Initialize And Use Pointer Variable.

/**
 * C program to create, initialize and use pointers
 */

#include <stdio.h>

int main()
{
    int num = 30;
    int * ptr;

    /* Stores the address of num to pointer type */
    ptr = &num;

    printf("Address of num = %d\n", &num);
    printf("Value of num = %d\n", num);

    printf("Address of ptr = %d\n", &ptr);
    printf("Value of ptr = %d\n", ptr);
    printf("Value pointed by ptr = %d\n", *ptr);

    return 0;
}

The above code produce the output like:

Output:
------
Address of num = 7655538
Value of num = 30
Address of ptr = 7655580
Value of ptr = 7655538
Value pointed by ptr = 30 

If you find any issue and interested to rewrite it then contact us.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 45

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply