# a random c pointer question

This topic is 4412 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I just came across a program used to test interviewees for their C pointer proficiency - it's listed here for your interest:
#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);
}


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?

##### Share on other sites
Quote:
 Original post by meatcowI 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 linen = *(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 ++).

##### Share on other sites
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).

##### Share on other sites
yeah, (p++) will increment after the statement has been evaluated, whereas (++p) will increment before.

##### Share on other sites
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.

##### Share on other sites
it's just the parentheses that were confusing me here;

so:
n = *p++
is exactly the same as
n = *(p++)

##### Share on other sites
Quote:
 Original post by meatcowit's just the parentheses that were confusing me here; so:n = *p++ is exactly the same asn = *(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.

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633318
• Total Posts
3011339
• ### Who's Online (See full list)

There are no registered users currently online

×