Thread: Beginner Question about Pointers in C

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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