Thread: newbie here

  1. #1
    Unregistered
    Guest

    newbie here

    could someone please explain the difference between:

    *ptr and ptr*



    thanks in advance

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Ho humm..
    Here are someexamples of the unary *...

    Code:
    int i = 6; // Declares an int named i (and gives it the value 6).
    int * iptr; // Declares an int pointer named iptr.
    
    // We can change the value of iptr...
    iptr = NULL;
    iptr = &i;
    iptr = malloc(sizeof(int));
    //Note that NULL, &i and the return of malloc are all memory
    // locations, not ints.
    
    
    // But to change the int that is is pointing to, we have to use *
    *iptr = 0;
    *iptr = i;
    *iptr += 3;
    *iptr = INT_MAX;
    The unary * is used for two purposes... first, we use it to declare pointers, second, we use it to access the memory pointed to by a pointer. There is no reason to have the * after a variable name, unless you are multiplying.
    Last edited by QuestionC; 12-19-2001 at 02:39 PM.
    Callou collei we'll code the way
    Of prime numbers and pings!

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    *ptr and ptr*

    I have seen the * used on the right side in type casting, ie:


    int comp(const void *i, const void *j)
    {
    return *(int*)i - *(int*)j;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getting to grips with allegro and ms vc++ (newbie)
    By jimjamjahaa in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 07:49 PM
  2. Newbie in problem with looping
    By nrain in forum C Programming
    Replies: 6
    Last Post: 11-05-2005, 12:53 PM
  3. Newbie Programmer
    By Extropian in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2004, 01:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 07:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 02:02 PM