Thread: Beginner Question about Pointers in C

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    28

    Beginner Question about Pointers in C

    I'm reading through a great book on C but it hasn't done a great job making pointers easy to understand. What I'm having trouble with is not the concept of pointers (although I'm still not sure yet how/why it will be useful in writing a program) but how the difference between the use of * and & in terms of pointers...

    from what i can tell * is only for declaring a variable as a pointer variable right? so...

    Code:
    int *myVariable
    float *myOtherVariable   //can a float do this?
    would both be valid right?

    what i'm confused about is when i refer to *myVariable from this point on do i need to use *myVariable everytime, or can I simply use myVariable (without the * infront of it) later in the program?

    ok, the other question is i saw an example in the book...

    Code:
    int *myVariable, myVariable2
    i'm going to assume myVariable2 is NOT a pointer variable because there is no * infront of it, right? or is it a pointer variable, because its on the same line as *myVariable?

    Ok, the 3rd and final question is about the uses of &...

    I assume you can put an & before any variable no matter what, and even if it hasn't been declared earlier as a pointer variable? and does an & mean you are taking the variable's address in memory from it instead of what the actual value store in the variable is?

    so would this be correct (would it work)...?

    Code:
    int *myPointerVariable;
    int boxOfBaseballs;
    
    int main( void ) {
    
    boxOfBaseballs = 24;
    *myPointerVariable = &boxOfBaseballs   //will store boxOfBaseballs memory address not "24"
    
    printf( "This address in memory is: %d", *myPointerVariable );
    
    return 0;
    }
    ... If the code above is valid, then why couldn't I just do this instead? (referring to the printf line) ...

    Code:
    printf( " This address in memory is: %d", &boxOfBaseballs );
    and another point... the pointer address in memory is going to an interger number so why bother with a *variable to store the information in? why wouldn't it make sense to simply say... someAddress = &whatever;
    Last edited by lucidrave; 08-01-2009 at 02:50 PM. Reason: fixing things up one more time

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The prefix '*' is used for declaring a pointer.

    If the '*' is present later in code, it used for accessing the value of that address (the data pointed to).

    I guess that's your confusion.

    Yes you can put '&' in front of variables no matter what. It will obtain the variable's address as opposed its value.

    Code:
    *myPointerVariable = &boxOfBaseballs
    Don't do that! There is no storage allocated for the destination. myPointerVariable points to who knows where. Not good. You haven't initialized it.

    Code:
    printf( " This address in memory is: %d", &boxOfBaseballs );
    Yes that's fine. However, a pointer may not have the same size as int necessarily. There is a printf format specifier designed specifically to display a pointer.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe your confusion stems from the fact that a pointer is a variable that contains a memory address!
    Since a pointer "points" to something, it must contain the address of that which it is pointing to.
    To access the stored address in the pointer, you would simply use it like a normal variable. To access what it points to, you prefix it with *.

    Quote Originally Posted by lucidrave View Post
    Code:
    int *myVariable
    float *myOtherVariable   //can a float do this?
    would both be valid right?
    Both are valid.

    what i'm confused about is when i refer to *myVariable from this point on do i need to use *myVariable everytime, or can I simply use myVariable (without the * infront of it) later in the program?

    ok, the other question is i saw an example in the book...

    Code:
    int *myVariable, myVariable2
    i'm going to assume myVariable2 is NOT a pointer variable because there is no * infront of it, right? or is it a pointer variable, because its on the same line as *myVariable?
    myVariable's type is int*, but myVariable2's type is int.
    Be careful with this. Declare each pointer on a separate row.
    Also, the * says it's a pointer, but it doesn't matter where you put it. These are all fine:
    Code:
    int* p;
    int * p;
    int*p;
    Ok, the 3rd and final question is about the uses of &...

    I assume you can put an & before any variable no matter what, and even if it hasn't been declared earlier as a pointer variable? and does an & mean you are taking the variable's address in memory from it instead of what the actual value store in the variable is?

    so would this be correct (would it work)...?

    Code:
    int *myPointerVariable;
    int boxOfBaseballs;
    
    int main( void ) {
    
    boxOfBaseballs = 24;
    *myPointerVariable = &boxOfBaseballs   //will store boxOfBaseballs memory address not "24"
    
    printf( "This address in memory is: %d", *myPointerVariable );
    
    return 0;
    }
    No, because you want to assign an address (the pointer's value) to the pointer first. By using * you actually dereference the pointer which contains no valid address.

    ... If the code above is valid, then why couldn't I just do this instead? (referring to the printf line) ...

    Code:
    printf( " This address in memory is: %d", &boxOfBaseballs );
    and another point... the pointer address in memory is going to an interger number so why bother with a *variable to store the information in? why wouldn't it make sense to simply say... someAddress = &whatever;
    First, use %x to print an address.
    Also, the benefit is not immediately obvious in this example, but when you pass arguments to a function, copies of those arguments are made. So if you want a function to CHANGE the value of a variable you pass in, you must pass a pointer, because otherwise the change is made to the copy!
    By using a pointer, it can modify what it points to. The pointer will be copied to, but its value is the variable it points to, so it's alright!
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ newbie question about pointers
    By lafayette in forum C++ Programming
    Replies: 15
    Last Post: 09-30-2008, 12:38 PM
  2. Pointers Question
    By HAssan in forum C Programming
    Replies: 2
    Last Post: 09-08-2008, 10:17 AM
  3. Utter beginner question about using notepad for c++
    By Borodin in forum C++ Programming
    Replies: 8
    Last Post: 08-18-2008, 04:08 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM