#include <stdio.h>
static int table[10] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
void main (void)
{
int *p, n;
p = table; // p = address of table
n = *p; printf("n = %d\n",n);
n = *p++; printf("n = %d\n",n);
n = *++p; printf("n = %d\n",n);
n = *(p++); printf("n = %d\n",n);
n = (*p)++; printf("n = %d\n",n);
}
a random c pointer question
I just came across a program used to test interviewees for their C pointer proficiency - it's listed here for your interest:
Im a little confused over the line
n = *(p++)
I thought it would increment p first, then be n would be assigned the value of *p - but i was wrong,
what is the reasoning for this?
Quote:Original post by meatcow
I just came across a program used to test interviewees for their C pointer proficiency - it's listed here for your interest:
*** Source Snippet Removed ***
Im a little confused over the line
n = *(p++)
I thought it would increment p first, then be n would be assigned the value of *p - but i was wrong,
what is the reasoning for this?
The statement p++ will increment the pointer *after* the current expression is finished.
So n = *(p++) will give to n the same value as *p. However, after that line, p points to the next element in table (because of the ++).
The post increment operator increases the varible by one and returns the previous value. Basically the same as
int post_increment(int &x){
int temp = x;
x = x+1;
return temp;
}
This is why some people say to use ++i instead of i++ since the former doesn't need to make a temporary. Any decent compiler can see that optimization (at least with primtives). Although it can make a difference on complicated classes (some smart pointers for instance).
int post_increment(int &x){
int temp = x;
x = x+1;
return temp;
}
This is why some people say to use ++i instead of i++ since the former doesn't need to make a temporary. Any decent compiler can see that optimization (at least with primtives). Although it can make a difference on complicated classes (some smart pointers for instance).
yeah, (p++) will increment after the statement has been evaluated, whereas (++p) will increment before.
Quote:
(some smart pointers for instance)
One would hope a smart pointer wouldn't provide an operator++ overload (in either form), as that would make it much less smart.
Iterators, however, are a typical example of where prefix increment might have a performance impact versus postfix increment.
it's just the parentheses that were confusing me here;
so:
n = *p++
is exactly the same as
n = *(p++)
so:
n = *p++
is exactly the same as
n = *(p++)
Quote:Original post by meatcow
it's just the parentheses that were confusing me here;
so:
n = *p++
is exactly the same as
n = *(p++)
Yes. The value of the expression
(p++)
is the same as the value of the expression
p++
which is to say, the value of p before the postincrement operator is applied. Applying the dereference operator to either expression will yield the same result.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement