A Beginner's Guide to Pointers

Discussion in 'C' started by Sanskruti, Jan 7, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India

    Introduction



    Definition: Pointer is a variable which stores address of another variable.

    To understand this definition properly, let us separate it into two statements:

    1. Pointer is a variable.
    2. It stores address of another variable.

    Consider the first statement, it means that a pointer is a variable like any other variable such as i in the statement int i, f in the statement float f, c in the statement char c etc. Now consider the second statement, it states pointer stores address of another variable. And this is the difference between pointer and an ordinary variable. An ordinary variable stores value such as a whole number, real number, a character etc, where as a pointer stores address of another variable.

    How pointers are declared and used?



    See the following statement in C programming language:

    int i = 327;

    say 3843 is the address (or say location number) in the memory where the value 327 is stored. Now to retrieve this value from the memory, we can use pointer.
    How? See this…..

    int * ptr; this is pointer declaration. here * is read as ‘value at address’, it denotes that variable ptr is not an ordinary variable but it is a special variable i.e it is a pointer variable

    ptr=&i; here & is ‘address of’ operator. It assigns address of i to ptr

    Get the value of the variable ptr (address of i) by using the statement

    printf(“%u”, ptr);

    Answer:
    3843

    If you want to see value stored at the address use following
    printf(“%d”, *ptr);

    Answer:
    327

    What is the use of pointers?



    All the beginners who are learning pointers wonder some time or the other, what is the use of pointers? The answer is, pointers are primarily used for constructing references, as well as in passing the data between different parts of a program.
    Pointers are used in languages such as C, Free BASIC, C++, Pascal, Fortran, and most assembly languages.

    Let us discuss pointers with reference to C programming language.



    1.Pointers can be dereferenced to access the data stored at the address pointed to, or to invoke the pointed-to function.

    2.Pointers can be manipulated using normal assignments and also pointer arithmetic.

    3.The run-time representation of a pointer value is typically a raw memory address, but since a pointer's type includes the type of the thing pointed to, expressions including pointers can be type-checked at compile time.

    Pointers are used for many different purposes in C.



    1.Text strings are commonly manipulated using pointers into arrays of characters.

    2.Dynamic memory allocation is performed using pointers.

    3.Pointers to functions are useful for callbacks from event handlers.
    A null pointer is a pointer value that points to no valid location (it is often represented by address zero).
    Dereferencing a null pointer is therefore meaningless, typically resulting in a run-time error.
    Null pointers are useful for indicating special cases such as no next pointer in the final node of a linked list, or as an error indication from functions returning pointers.
    Void pointers (void *) also exist and point to objects of unknown type, and can therefore be used as "generic" data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them possible, although they can easily be (and in fact implicitly are) converted to and from any other object pointer type.
     
    1 person likes this.
  2. Peter_APIIT

    Peter_APIIT New Member

    Joined:
    Apr 11, 2007
    Messages:
    92
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Malaysia
    Hello Sanskruti, you should start a new topic in c how to dynamic allocate memory because this features is quite useful and enhance flexibility of the program.

    By the way, how to dynamic allocate memory for array of pointer and each pointer pointed to integer.

    For example, int *array_ptr[10] contain 10 pointer and each pointer pointing to integer.


    Thanks for oyur help.

    Your help is greatly appreciated by me and others.
     
  3. tailhook123

    tailhook123 New Member

    Joined:
    May 23, 2007
    Messages:
    30
    Likes Received:
    0
    Trophy Points:
    0
    The [n] form for array should automatically allocate the 10 pointers. Just like:

    int a[10]

    will automatically allocate 10 integers.

    If you're referring to the integers themselves... you just allocate them before they're set.

    int my_int;
    int *ptr_int;

    array_ptr[1] = &my_int; // & returns the address of the variable.

    ptr_int = new int *; // if i remeber that properly
    array_ptr[1] = ptr_int;

    That said.. all this stuff is ancient code heh. New causes so many problems.. especially when you allocate something and then forget to free the space.
     
  4. karri

    karri New Member

    Joined:
    Jun 5, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    main()
    {
    int a[5] = {1,2,3,4,5};
    int *ptr = (int*)(&a+1);

    printf("%d %d" , *(a+1), *(ptr-1) );

    }


    in this code though &a is a pointer to pointer it is assigned to a pointer (ptr) by typecasting how is it possible;
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    karri, It looks like your post is not related to the above article and I would suggest you creating a new thread with appropriate title for your query.
     
  6. sgs_guna

    sgs_guna New Member

    Joined:
    Oct 23, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This info abt pointers would impress Beginners more...

    Also pls try 2 post more topics 4 beginners 2.....

    Thanx...
     
  7. vinay121

    vinay121 New Member

    Joined:
    Jun 26, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i want video lecture to learn pointer and passing pointer to function... could u tell me site?
     
  8. sumtaru

    sumtaru New Member

    Joined:
    May 17, 2012
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    good one for beginners. I want more examples.
     
  9. samraj

    samraj New Member

    Joined:
    Jan 6, 2013
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    I am having a structure containing 80 fields in one file.
    an extern structure in other file.

    i am losing variable values between function calls.

    how pointers can help me to solve this.???
     

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