Thread: Question about pointers

  1. #1
    Registered User
    Join Date
    Feb 2013
    San Jose, California, United States

    Question about pointers

    From what I understand about pointers:

    int k = 5;   // create a new variable k with a value of 5
    int *ptr;   // create new pointer ptr
    ptr = &k;   // make ptr point to the address of k
    *ptr = 7;   // assign the value of 7 to the pointed address
    But I saw other code like this which works:

    int *ptr = &k;
    I don't understand this because I thought that you can only assign a variable to *ptr and an address to ptr, but &k is an address of k. Can anyone explain this to me?
    Last edited by vxs8122; 03-05-2013 at 10:21 PM. Reason: made a mistake on 2nd line

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    This is wrong:
    int ptr*;
    You were probably thinking of:
    int *ptr;
    Quote Originally Posted by vxs8122
    I don't understand this because I thought that you can only assign a variable to *ptr and an address to ptr, but &k is an address of k. Can anyone explain this to me?
    It may look like you are initialising *ptr with the address of k, but actually you are initialising the pointer with the address of k.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    In addition to laserlight's answer you have to be aware that the asterix "*" has different meanings in C depending on the context. When used in a declaration it is not the operator for dereferencing.

    Bye, Andreas

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    int *ptr = &k;
    is just a short-hand, admittedly confusing if you're not used to it. It declares a pointer (int *), and then immediately initialises it with the address of k. It doesn't dereference (the term for when you use a pointer to get the data out of whatever the pointer points at) ptr at all.

    Where the asterisk(*) is makes a big difference, and here it's in the middle of declaration. Take the "int" off (i.e. declare it to be a pointer to an int somewhere else in the program) and it would become a very different matter. If you were to (mentally) replace the * with two symbols instead - one when you mean "please make this a pointer variable" and one when you mean "give me what's stored where this pointer points" - then you would see more clearly what the problem was. Unfortunately, C decided to use the same symbol for two very different jobs, depending on the context.

    But, hey, in English the word "set" has something like 27 definitions depending on the context, so it's just human nature.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about pointers.
    By november1992 in forum C Programming
    Replies: 4
    Last Post: 09-01-2012, 04:05 PM
  2. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  3. A question about pointers in C
    By Kilua in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 02:33 AM
  4. Pointers to pointers question
    By mikahell in forum C++ Programming
    Replies: 10
    Last Post: 07-22-2006, 12:54 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM