Jump to content
  • Advertisement
Sign in to follow this  
meatcow

a random c pointer question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 ++).

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!