Thread: help with pointers

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    2

    help with pointers

    i have a question.
    why do we need to declare pointers as int *p when int p also does the same work?
    eg:
    Code:
    //eg1
    int a=10, *p;
    p=&a;
    printf("%p", p);
    //eg2
    int a=10, p;
    p=&a;
    printf("%p", p);
    both give the same result

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    The 2nd example won't even compile. 'p' is an int, not a pointer to int.

    There is a difference in the declaration of a pointer and the use of a pointer.
    That may be where the confusion is. The '*' is used when declaring a pointer,
    but not used when referencing the value of a pointer. The '*' is used when
    dereferencing the pointer, to access what the pointer is pointing to.

    In a declaration:
    int p - is an int
    int *p - is a pointer to int

    In usage:
    p is a pointer
    *p is what p points to
    Last edited by megafiddle; 11-24-2011 at 06:29 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    re

    Although you can get the memory address just fine with this method, you wouldn't be able to dereference to get the value without declaring it as a pointer. With eg2 try changing the printf statement to:
    Code:
    printf("%d", *p);
    You will get a compiler error on this one, thus the value isn't retrievable and the pointer is pointless.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by parthpk92 View Post
    i have a question.
    why do we need to declare pointers as int *p when int p also does the same work?
    eg:
    Code:
    //eg1
    int a=10, *p;
    p=&a;
    printf("%p", p);
    //eg2
    int a=10, p;
    p=&a;
    printf("%p", p);
    both give the same result
    For most things you don't need pointers... in fact there is some wisdom in avoiding them as much as possible.

    However, they do have their uses... For example I routinely deal with very large strings, text files loaded as one big piece, into memory that is allocated with malloc(). Then I will take with strtok() and build an array of pointers to the beginning of each line... None of it is the actual string but it does tell me where the strings are.

    What a pointer does is allow you to store the address of something... not it's value, it's location... For example: "The house at 37 anystreet has a red door"... the pointer stores the address of the red door (37 anystreet) but not the red door itself. To see the red door you need to go to the address, or in computer terms you need to "dereference" the address to get at the data.


    @megafiddle... The second example will compile and it will get the address of the data in a ... The integer variable p becomes just a number that happens to be the same as the address of a that will be utterly useless as a pointer.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Thanks for correcting.

    My stuff fails to compile so often, that I don't distinguish between errors and warnings anymore..

    But it all gets fixed in the end.
    Last edited by megafiddle; 11-24-2011 at 06:48 PM.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    2
    thanks everyone!
    especially Crptix for a lucid explanation. i'm just starting out so your simple was extremely helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  2. Storing function pointers in generic pointers
    By Boxknife in forum C Programming
    Replies: 6
    Last Post: 08-01-2009, 01:33 PM
  3. Pointers to objects -- passing and returning pointers
    By 1veedo in forum C++ Programming
    Replies: 4
    Last Post: 04-04-2008, 11:42 AM
  4. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  5. weak pointers and use_count smart pointers
    By Mario F. in forum C++ Programming
    Replies: 2
    Last Post: 07-29-2006, 07:54 AM

Tags for this Thread