Thread: Understanding Pointers - Easy Questions.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    Understanding Pointers - Easy Questions.

    I'm having so much trouble grasping pointers. I'm experimenting with pointers and I expect to ask several questions on this thread. My first confusion is notation.

    I've gotten over declarations at this point. If I do

    Code:
    int main ()
    
    {
      int *ptr;
    }
    ptr is a pointer. But I'm confused about using it in code. When I use ptr, there are times when I don't include the star and times when I do. How can I know when to use it?

    for instance, given that *numPtr is declared:

    *numPtr = malloc(sizeof(int));

    does not work, but

    numPtr = malloc(sizeof(int));

    does work. So here I need to use the star char.

    But when I use the equal operator, I cannot use the star symbol. Why not?

    PtrPtr = numPtr (correct)

    printf("%i\n", *PtrPtr); (requires a star, won't work without it. Why not?)

    Please help me actually understand this, not memorize it.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Just remember "declaration follows usage".

    If you have the declaration
    Code:
    int *ptr;
    in the usage ptr, it is a pointer to int; the usage *ptr, it is an int;

    So, having as in your example: int *numPtr; we need

    Code:
    numPtr = malloc(sizeof (int)); /* numPtr is a pointer, which is right for the return value of malloc() */
    PtrPtr = numPtr; /* numPtr is a pointer, which is the same type as PtrPtr */
    *numPtr = 42; /* assign the int 42 to a memory location that can hold ints */
    printf("%i\n", *numPtr); /* *numPtr is an int */

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Let's work with the following declaration
    Code:
    int x = 6;
    in* p;
    p is a pointer.It is ready to point to variable of type int.
    In your computer, memory is allocated so that there an address can be stored.
    When you say
    Code:
    p=&x;
    you assign the address of the variable x to the memory that was allocated for the pointer.
    Now how to read the operators * and & for a random variable a:
    Code:
    &a = the memory address where a is stored
    *a = the content of the memory address that a is pointing to
    When we want to set a pointer to point in the same place another pointer is pointing to, we have to copy the address of the "direction" we want our pointer to get.
    So we do this
    Code:
    /* Declarations */
    int x = 6;
    int* p;
    int* d;
    
    /* Set p to x */
    p = &x;
    
    /* Now set d to point where p is pointing to */
    d = p;
    malloc returns a pointer, that's why you have to use it like this

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    malloc returns a pointer, that's why you have to use it like this
    That's not the reason.

    Anyway I have this to add: If a pointer points to memory not returned by malloc/realloc/calloc, you can't call free().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbi Questions, A Deeper understanding of C
    By robrob in forum C Programming
    Replies: 1
    Last Post: 04-15-2012, 05:52 PM
  2. Two easy vector questions.
    By Pecado in forum C Programming
    Replies: 8
    Last Post: 10-17-2010, 06:22 PM
  3. Two Easy Questions
    By CougarElite in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2005, 09:58 PM
  4. two easy questions
    By ... in forum Windows Programming
    Replies: 12
    Last Post: 03-27-2003, 09:00 AM
  5. A few easy questions
    By pdstatha in forum C Programming
    Replies: 3
    Last Post: 03-29-2002, 10:41 AM