Thread: Pointers make sense

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    1

    Pointers make sense

    Reading pointers finally make sense and I just want to share and be told off if I'm wrong. It's all down to the names and the equals sign!
    So,
    & gives the reference,
    * is the dereference.
    These are inverse operators so applying both is like doing nothing. A lot like multiply by 2 and divide by 2 or add two and subtract 2. More formally, a/a = 1 and 1 + a - a = 1. So following on *& = 1.

    Consider the snippet from Cprogramming.com pointers,

    Code:
    int x; // A normal integer
    int *p; // An pointer to an integer
    p = &x; // Assign the address of value x to p cin >> x; // Give x a value cin.ignore(); // Remove the carriage return from the entry cout << *p << "\n"; // This will print the value entered for x
    Now the bit that clicked for me was this,
    Code:
        p = &x
    so if I apply the * operator, I must apply to both sides
    Code:
        *p = *&x
    but the operators are inverses on the right hand side
    Code:
        *p = x
    the last line would act like
    Code:
    cout << x << "\n";
    and it does.
    Hopefully someone more practiced can say if this is a good schematic view. I've not seen this mentioned elsewhere and hope it helps someone who is stuck with pointers but thinks like a mathematician, got to be one or two out there.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I suppose that works. Just remember that & gives the address, not the reference (reference is an entirely different thing).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    It seems that you are know learning about pointers.I am i student(i am learning too) so when i want to be sure about pointers and references i use a paper and a pencil.I draw small boxes that stand for memory cells.Then i draw arrows(from boxes that are hosting pointers) to point in other boxes.However because an image equals to 1000 words,see that
    Code:
    int x=2;
    int *p;
    p=&x;
    Image(my intention was to upload one but it was too large)
    Let adress of x to be 100

    p x
    +----+(space.)+--+
    |100 |-------->| 2 |
    +----+(space.)+--+


    So if you follow this tactic then the most compilcated issues will be resolved easily
    Last edited by std10093; 07-14-2012 at 08:57 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does this make any sense?
    By xniinja in forum C Programming
    Replies: 20
    Last Post: 08-05-2010, 11:07 AM
  2. does it make any sense
    By ExDHaos in forum C++ Programming
    Replies: 0
    Last Post: 05-23-2009, 08:06 AM
  3. Can anyone make sense of this?
    By tjpanda in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2007, 02:16 PM
  4. Can anyone help me make sense from this?
    By correlcj in forum C Programming
    Replies: 1
    Last Post: 07-23-2002, 07:31 PM
  5. anyone make any sense of this
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2001, 10:29 PM

Tags for this Thread