Saturday, December 5, 2009

Pointer indirection with care




* and . operator
struct rec
{
int i;
float f;
char c;
};

int main()
{
struct rec *p;
p=(struct rec *) malloc (sizeof(struct rec));
(*p).i=10;
}
To initialise pointer to 10, we have to use parenthesis, because . operator has higher precedence than * operator.

++ and *
Consider *p++
The postfix "++" operator has higher precedence than prefix "*" operator. Thus, *p++ is same as *(p++); it increments the pointer p, and returns the value which p pointed to before p was incremented. If you want to increment the value pointed to by p, try (*p)++. 

No comments:

Post a Comment