newbie here

This is a discussion on newbie here within the C Programming forums, part of the General Programming Boards category; could someone please explain the difference between: *ptr and ptr* thanks in advance...

  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, 02:17 PM
  4. Some help for a newbie?
    By Ilmater in forum C++ Programming
    Replies: 23
    Last Post: 04-19-2004, 08:44 PM
  5. Newbie Game Develpoers Unite!
    By Telenosis in forum Game Programming
    Replies: 10
    Last Post: 06-22-2002, 03:02 PM

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