Maker Pro
Maker Pro

Pointer and it's use

Generally I know pointer is use to store address of variable. I can write basic program for pointer but I am still confuse I don't understand actually what's the real real use of pointer and where we will use pointer in programming.

Code:
#include <stdio.h>
 
int main(void)
{
    int variable = 10;
    
    int *pointer = &variable;
 
    printf("The value of variable is: %d \n", variable);
    
    printf("The address of variable : %p \n", &variable);
    
    printf("The value of pointer    : %p \n", pointer);
 
    printf("The address of pointer  : %p \n", &pointer);
 
    
    return 0;
}

Compiler output

The value of variable is: 10
The address of variable : 0061FF2C
The value of pointer : 0061FF2C
The address of pointer : 0061FF28

int variable = 10; // address = 0061FF2C, data = 10

int *pointer = &variable; // address = 0061FF28, data = 0061FF2C

How do you know that you have to use pointer in program and where do we use pointer in program?
 
Well,
A pointer is a very important tool in Data Structures.
Pointers are very helpful in creating,manipulating and searching "linked-lists".
The pointer is the connecting item of the list,it does that by pointing from one list item to another one.
Here is a starting point.
 
Last edited:

Harald Kapp

Moderator
Moderator
Why use pointers over normal variables?
E.g. Because a pointer can access a specific address (e.g. for I/O). A variable is assigned an arbitrary memory location, you cannot use it to access a wel defined address.
Another reason is the use of dynamic memory allocation, e.g. when while writing the program you don't know how much memory you will need at runtime. Therefore you cannot define variables for this unknown memory, you need to allocate it at runtime and manage it using pointers.
 
E.g. Because a pointer can access a specific address (e.g. for I/O). A variable is assigned an arbitrary memory location, you cannot use it to access a wel defined address
Sorry but still I am confuse. If I compare my program with your answer I don't understand what's happening here. I don't know where I am doing mistake
 

Harald Kapp

Moderator
Moderator
I don't know where I am doing mistake
Which mistake? There is none.
1. you print the value of the variable which is 10 -> o.k.
2. you print the address of the variable whic is 0061ff2c -> o.k.
3. you print the value of the pointer which is 0061ff2c, as it points to the location where the variable is stored -> o.k.
4- you print the address of the pointer which is 0061ff28 -> o.k.

The pointer's address is different from the pointer's value as you need a memory location (61ff28) to store the value (61ff2c). All is well.
 
Which mistake? There is none.
The pointer's address is different from the pointer's value as you need a memory location (61ff28) to store the value (61ff2c). All is well.

Look at this program This program store array element using pointer
Code:
#include <stdio.h>

int main(void)
{
    int i;
    int array[5] = {6, 2, 3, 9, 5};
    int *pointer = array;     
    for (i = 0; i < 5; i++)
    {
        printf("%d", *pointer);
       
      pointer++;
    }
   
    return 0;
}

I have seen in many places pointer is use to store array element. we can store element using array or just use pointer to store array element.

I don't understand what's the benefits of pointer in this program
 

Harald Kapp

Moderator
Moderator
In this example pointer and array are used interchangeably. You could have used array instead of the pointer within the loop. There is no rule to tell you which one to use.
It is easy for the compiler or a checking tool (e.g. lint) to do some static checks for the validity of the array index (with suitable compiler options active) since it knows from the definition of array[5] that only array indices 0...4 are valid.
It is a lot more difficult to check the validity of the address where pointer points at. It is therefore much easier to access illegal memory locations using pointers.
 
Top