Trying To Understand Pointers!

This is a discussion on Trying To Understand Pointers! within the C Programming forums, part of the General Programming Boards category; Hi there guys, I've been reading about pointers lately as they're one of the most important things to become comfortable ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230

    Trying To Understand Pointers!

    Hi there guys, I've been reading about pointers lately as they're one of the most important things to become comfortable with. I think I have the main idea down, but I still have a bit of confusion, so I'd like to ask a couple of questions.

    Code:
    #include <stdio.h>
    
    int *pCake;
    int dog;
    
    int main()
    {
    dog = 25;
    pCake = dog;
    or
    pCake = &dog;
    }
    #1 "pCake = &dog" - pCake's value is now dog's address, right? Therefore pCake is now a pointer to dog.

    But:

    #2 "pCake = dog" - Is this the same as #1? Does this set pCake's value to dog's address just as it does in #1? Or does this infact set pCake's value to 25?

    If this is the case, derefercing pCake (if pCake = dog not &dog) would point to an address of "25" and print the variable/value that is being held at the address "25"? But since "25" isn't a valid address, it wouldn't work, therefore what would be the point of using "pCake = dog" instead of "pCake = &dog"? Do they do the same thing, does the compiler automatically put the "&" there to specify address?

    I've been thinking about this and, maybe if the code was like this it would be useful, but I can't see a use for this:

    Code:
    #include <stdio.h>
    
    int *pCake;
    int dog;
    
    int main()
    {
    int dog = 0x0000204; 
    /* random address in this examplebut maybe it actually points to 
    something in reality, i.e a variable used elsewhere. */
    int pCake = dog;
    }
    Now pCake's value should = dog's value? Am I correct? Therefore pcake is equivalent to 0x0000204, which is basically the same as a pointer, but instead of it pointing to dog, it's infact pointing to another variable (which I haven't set in this example).

    I've tried to explain what I'm talking about best I could but it's pretty confusing so yeah. Basically I want to know if pCake = &dog; and pCake = dog; are the same (maybe the compiler automatically assumes dog's address - not it's value, because it's dealing with a pointer - or maybe not, please let me know), also if they aren't the same, then what use would leaving out the & have.. Thanks! hehe

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    #include <stdio.h>
    
    int *pCake;
    int dog;
    
    int main()
    {
    dog = 25;
    pCake = dog;
    or
    pCake = &dog;
    }
    The first one sets the address of pCake to 25. The second one is the correct way to assign the pointers address.

    Code:
    #include <stdio.h>
    
    int *pCake;
    int dog;
    
    int main()
    {
    int dog = 0x0000204; 
    /* random address in this examplebut maybe it actually points to 
    something in reality, i.e a variable used elsewhere. */
    int pCake = dog;
    }
    There is no use of pointers at all in this one.... pCake is an int not an "int*"...its a separate variable.
    Last edited by 39ster; 05-07-2008 at 10:06 PM.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,698
    You should get a compiler error if you have two variables named pCake.

    Now if dog were a pointer, assignment would store an address.
    Code:
    #include <stdio.h>
    
    int main (void)
    {
       int data;
    
       int * dog = &data;
       int * pd;
       pd = dog;
       printf("pd is address &#37;p\n", (void*) pd);
       printf("data is at address %p\n", (void*) dog);
       return 0;
    }
    Like that.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,594
    I would note that if you have
    int *pCake;
    int dog;
    then pCake = dog won't compile because pCake = int* and dog = int.
    You must always take the address of a variable to assign to a pointer (exceptions are arrays which are automatically treated as pointers without the & when assigned or passed as arguments).

    I don't know if this link helps you, but it might be worthwhile reading anyway:
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Australia
    Posts
    230
    Ok, pretty sure I understand now, Thanks!

Popular pages Recent additions subscribe to a feed

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