The variable
i_ptr
is a pointer to integer so it is declared a type of
int *
. Using the asterisk with a pointer makes it the dereference operator so it is obtaining the value at that address.
See this page for an operator precedence chart :
C Operator Precedence - cppreference.com[
^]. This statement there is very important :
Quote:
When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression *p++ is parsed as *(p++), and not as (*p)++.
The expression you are asking about is actually parsed as if it looked like this :
*(i_ptr++)
which means the pointer is incremented to point to the next item and then it is dereferenced. Equivalent code to that would look like this :
for (count = 0; count < MAX; count++)
{
i_ptr++;
f_ptr++;
printf("%d\t%f\n", *i_ptr, *f_ptr );
}
which means the output would start with the values of the second items or
i_ptr[1]
and
f_ptr[1]
. This means the code will access data beyond the bounds of the array which is a bad thing. To avoid that you could increment the pointers after displaying their values. You can do that in at least two ways. One is :
for (count = 0; count < MAX; count++)
{
printf("%d\t%f\n", *i_ptr, *f_ptr );
i_ptr++;
f_ptr++;
}
OR
for (count = 0; count < MAX; count++)
{
printf("%d\t%f\n", (*i_ptr)++, (*f_ptr)++ );
}
The second option deferences the pointer first and then the pointer is incremented.
Personally, I avoid writing code like this that relies on the precedence order. I prefer to make the order of operations explicit and not use compound operations, like the first option does.