Connect with us

How to learn pointer in easy steps

Discussion in 'Electronics Homework Help' started by anukalp, Aug 13, 2018.

Scroll to continue with content
  1. anukalp

    anukalp

    33
    0
    Jul 28, 2018
    I’m fairly new here so I'm sorry if this question is not suitable on this forum. I found pointer a little difficult to understand in . Pointer in C language is a variable that store the address of another variable.

    Let’s start with a simple program to define a integer type variable and a pointer variable to store it’s address

    Code:
    int main()
    {
        /*declaration of integer variable*/
        int variable = 20;
       
        /*declaration of integer pointer*/
        int *pointer;      
     
        /*assigning address of variable*/
        pointer = & variable ;
         
       return 0;
    }

    pointer = & variable
    pointer is storing address where variable value 20 is stored

    I learn how to declare and initialize pointer in c. but I don't understand proper concept? I am looking someone's help to get grasp knowledge on pointer in c language.
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    11,517
    2,651
    Nov 17, 2011
  3. Ratch

    Ratch

    1,094
    334
    Mar 10, 2013
    A pointer is the same in any language. I don't understand your problem or question. You first define correctly what a pointer is, and then give an example of how to implement it in the C language. That is like an experienced taxi driver asking what the brake and gas pedals of a car do. What specifically is your question?

    Ratch
     
  4. anukalp

    anukalp

    33
    0
    Jul 28, 2018
    Harald Kapp, thanks for the response!
    I have been gone through this website before posting question
    I understand how to declare and initialize pointer in c language.I have mentioned my understanding in my post. but I feel I don't understand proper concept behind the use of pointer.

    When you solve the problem, when do you think the pointer should be used in this place.

    1. What is the problem statement for pointer in c
    2. I want to solve the one problem with pointer and without pointer for same purpose
     
  5. Ratch

    Ratch

    1,094
    334
    Mar 10, 2013
    A pointer is simply an address. It does not have a problem statement. Using pointers is only limited by your imagination For instance, you can declare a table of pointers and index into the table for a specific data word that the pointer addresses.

    Ratch
     
  6. BobK

    BobK

    7,682
    1,688
    Jan 5, 2010
    Think about a language without pointers. Lets say you need to write a program that uses a lot of data. Say data about 1 million people. You could have an array declared with 1 million entries, but what if it becomes 2 million people later on? How can you write a program without pointers that can deal with an amount of data that is not known when you write the program? In C without pointers, you would have to declare an array, but how would you decide what size? Whatever size you declare might become too small some time in future.

    Pointers solves this problem. For each person, you get a new chunk of memory. But how do you refer to the new chunk? With a pointer! The c function malloc() does this. It gets a new chunk of unnamed memory and returns a pointer to it. Now, let's say part of the information you want about a person is her parents and siblings. How you you deal with that? For the parents it is easy, the information (struct) about the person contains one pointer for the mother and one for the father. The value of these pointers is the address of the chunk of data for the parent. The data does not need to be duplicated for each sibling, because the data chunk for each siblink point to the same mother and father chunks.

    How about siblings? There is an unknown number of them, you you can't just have an array. Why not have a pointer that points to the first sibling, and a pointer that points to the next sibling in each data chunk? Now we can get to all of a set of siblings from each sibling by following the next pointer. The next pointer of the last sibling could be made to point to the first sibling, making it a circular list.

    Pretty much all complex programs use data structures like I have described above, and there are many more types of data structures, normally linked together with pointers.

    Bob
     
  7. anukalp

    anukalp

    33
    0
    Jul 28, 2018

    Consider Program to store five integer value

    Value : 1 2 3 4 5

    program to store five integer using array
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i;
        int a[5] = {1, 2, 3, 4, 5};
       
        for (i = 0; i < 5; i++)
        {
            printf(" Value : %d ", a[i] );    
        }
       
        return 0;
    }
    program to store five integer using array pointer
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i;
        int a[5] = {1, 2, 3, 4, 5};
        int *p = &a[0];    
        for (i = 0; i < 5; i++)
        {
            printf("%d ", *p);
            p++;
        }
       
        return 0;
    }
    If we compare both programs do you tell me any advantage of using pointer
     
    Last edited: Aug 16, 2018
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.
Similar Threads
Loading...
Electronics Point Logo
Continue to site
Quote of the day

-