Connect with us

Pointer and it's use

Discussion in 'Microcontrollers, Programming and IoT' started by vead, Jan 8, 2018.

Scroll to continue with content
  1. vead

    vead

    473
    14
    Nov 27, 2011
    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?
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
  3. dorke

    dorke

    2,342
    665
    Jun 20, 2015
    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: Jan 8, 2018
  4. vead

    vead

    473
    14
    Nov 27, 2011
    I have gone through this discussion but still have confusion. I wanted to know when do you feel in program that you have to use pointer and what's the reason behind it.

    Why use pointers over normal variables?
     
  5. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
    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.
     
    vead likes this.
  6. vead

    vead

    473
    14
    Nov 27, 2011
    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
     
  7. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
    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.
     
  8. vead

    vead

    473
    14
    Nov 27, 2011
    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
     
  9. Harald Kapp

    Harald Kapp Moderator Moderator

    10,025
    2,138
    Nov 17, 2011
    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.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-