Introduction to Pointers in C

Discussion in 'C' started by lionaneesh, Mar 6, 2012.

  1. I am doing C programming from a couple of years ago and finally I convinced myself and grabbed all my guts to write an article on Pointers. Now, one could easily question Why? Basically, Pointers are the most useful part of C and usually the most difficult to understand.

    Note: I don't want to discourage my readers, that learning about pointers can be a difficult but that's what most of the programmers say and according to me its the most exciting part of C Programming.

    For the scope of this tutorial we'll only be looking at how to declare a pointer? , and the most basic pointer operators.

    What is a Pointer



    A pointer is a variable that contains the address in memory of another variable. We can have pointers for any data type in C, for example a long pointer , int pointer, char pointer etc. We also have a void pointer which does not have a type, thus it can be used to point to any data-type or even any kind of data, for the scope this tutorial we'll be not looking into any operations on void pointers. Void pointers are a special type of pointers in C and they need to be used in a different way which is out of the scope of this tutorial.

    Operators



    The unary operator '&' gives the adress of a variable.

    Demonstration :-

    Code:
    #include<stdio.h>
    
    int main()
    
    {
    
    int x = 0;
    
    printf("Address of x: %p\n", &x);
    
    }
    
    
    Output :-

    Code:
    
    Address of x: 0xbf912a5c
    
    
    Which would obviously give out a different address on different executions (Due to ASLR).

    The dereference operator i.e * gives the contents of the object pointed by a pointer.

    Declaring a pointer :-

    To declare a pointer we follow the following format :-

    Code:
    
    [TYPE] *[NAME]
    
    
    of course that's not a strict format and there are a number of ways to declare a pointer but for the scope of this tutorial, we'll be sticking to this one.

    example :-

    Code:
    
    int *ptr
    
    
    The above code declares an int pointer named “ptr”.

    Note: Same as any C variable pointer also points to a garbage value (i.e address in case of pointers), and should always be initialized before using. Using uninitialized pointers can cause reading memory from an arbitrary address which will eventually result in a Segmentation fault i.e Invalid Reads or Writes depending on the usage.

    Working with Pointers



    example.c
    Code:
    #include<stdio.h>
    
    int main()
    
    {
    
    int *ptr;
    
    int x = 0;
    
    ptr = &x;
    
    printf("x : %d\n", *ptr);
    
    }
    
    Note: We must associate the pointer to its same type, i.e A long pointer should be associated with a long variable, and not with any other data type.

    Consider a set of strict boxes, in which only a specific type of item can be contained, for instance a square box cannot hold a rectangular object. Similar is the case with the pointers. (Again I am not taking Void pointers into consideration as of this tutorial.)

    Before reading the explanation below or before compiling, try to figure out what should be the output of the above program.

    Output :-
    Code:
    
    x : 0
    
    
    Explanation :-


    5th line: Here we declare an int pointer named ptr.
    6th line: Here we declare an int variable x and initialise it to 0.
    7th line: Here we use the unary operator '&' to get the address of x and store it in ptr.

    Now the pointer ptr contains the address of x, now the value of x can be accessed using the dereferencing operator.

    9th line: Here we use the derefrencing operator which gives the value of the variable pointed by our pointer i.e x.

    Now that might be a little difficult to grasp at first but believe me its easier than it looks.

    Exercises :-

    In this tutorial we were inroduced to basic usage of pointers in C, now I'd like the viewers to write some basic programs to test their knowledge.

    • Write a program to print the value of a long variable using pointers.
    • Write a program to print the value of a char variable using pointers.
     
    pawansharma, samantha1230 and shabbir like this.
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    Good tut man! Keep it up!
     
  3. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    Thanks :)
     
  4. alaziaonline

    alaziaonline Banned

    Joined:
    Mar 14, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.alazizonline.com
    you shared a very informative information here thanks a lot for sharing
     
  5. Davisandrian

    Davisandrian New Member

    Joined:
    Mar 15, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.edwardhighschool.com/
    Read your tut, its really helpful. Thanks for spreading.
     
  6. samantha1230

    samantha1230 Banned

    Joined:
    Mar 28, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Me too get every time confused with pointers. Thats good at-least after long time you successfully understand about pointers. Theoretically they can be understood easily but at time of practical I became dumb.
     
  7. honeylamb23

    honeylamb23 New Member

    Joined:
    Mar 30, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    hair stylist
    Location:
    usa
    Home Page:
    http://www.tanyasimage.com/
    C programming is the hardest programming language I've tried..
     
  8. tinytomhanks

    tinytomhanks Banned

    Joined:
    Apr 20, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    business admin
    Location:
    US and India
    Home Page:
    http://www.itchimes.com/
    I am also start using c, i find it very resourceful. your telling style is very good.
     
  9. sandeep552

    sandeep552 Banned

    Joined:
    Mar 23, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Nice one, thanks for introduce pointer.
     
  10. tinytomhanks

    tinytomhanks Banned

    Joined:
    Apr 20, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    business admin
    Location:
    US and India
    Home Page:
    http://www.itchimes.com/
    I feel happy to be here again, well as I know pointer is nothing but a variable which contain the address of others variables.
     
  11. pawansharma

    pawansharma New Member

    Joined:
    May 7, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Web developer
    Location:
    Delhi
    Excellent example how to use pointer in c.
     
  12. Healthcare

    Healthcare Banned

    Joined:
    Jun 14, 2012
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    I am not sure about the task, but i can try. If you find any mistake, please let me know with explanation.
    Code:
    #include<stdio.h>
    
    void main()
     {
           long *ptr;          //declaration of pointer variable
           int x=0;             //declaration and initialization of a variable
           ptr=&x;
           printf("The value of x is:" *ptr);
     }
     
    Last edited by a moderator: Jun 14, 2012

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice