Like Tree1Likes
  • 1 Post By laserlight

Question about pointers

This is a discussion on Question about pointers within the C Programming forums, part of the General Programming Boards category; From what I understand about pointers: Code: int k = 5; // create a new variable k with a value ...

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

    Question about pointers

    From what I understand about pointers:

    Code:
    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:

    Code:
    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 09:21 PM. Reason: made a mistake on 2nd line

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    This is wrong:
    Code:
    int ptr*;
    You were probably thinking of:
    Code:
    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.
    Matticus likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    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
    Posts
    435
    Code:
    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, 10:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21