could someone please explain the difference between:
*ptr and ptr*
thanks in advance
Printable View
could someone please explain the difference between:
*ptr and ptr*
thanks in advance
Ho humm..
Here are someexamples of the unary *...
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.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;
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;
}