• Advertisement
Sign in to follow this  

c Programming - Pass int * to func, return next int *?

This topic is 2776 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 was doing something as simple as

int * nextToken(int * currentToken) {
return currentToken++;
}


it however doesn't change the int array pointer outside the func scope. It seems like I should be passing a pointer to the pointer for the current token, deref the current token, incrementing the curr token, then returning the deref token?

Share this post


Link to post
Share on other sites
Advertisement
"It seems like I should be passing a pointer to the pointer for the current token, deref the current token, incrementing the curr token, then returning the deref token?"

How does that work? That is, have you tried it and how did it work?

What about ...

passing a pointer to the pointer for the current token
incrementing the pointer to the pointer for the current token
deref the pointer to the pointer for the current token
return the deref?


Share this post


Link to post
Share on other sites
Try using return ++currentToken; or return(currentToken + 1); in your function instead. currentToken++ increments the value AFTER it is used.

Share this post


Link to post
Share on other sites
Quote:
It seems like I should be passing a pointer to the pointer for the current token, deref the current token, incrementing the curr token, then returning the deref token?


Yes you can do this to increment the pointer directly. Or just use ++currentToken like fun2code said.

Incrementing the pointer directly looks like:


void nextToken(int ** currentToken) {
(*currentToken)++; // No need to return a value
}

Share this post


Link to post
Share on other sites
Hello.

Your problem is that you are using call-by-value. That means (even if your function's argument is a pointer to, say, memory address X) that it is copied on calling the function. So, when you refer to currentToken inside the function you are using a copy of the pointer defined in the scope outside the function. This pointer also refers to address X, but if it is incremented to X+4 (assuming 4 is the size of an int on your system), the pointer from the outside scope still refers to location X in memory.

To avoid this, switch to call-by-reference, like the following example does:

int* nextToken(int*& currentToken) {
return ++currentToken;
}


I am not sure whether pre- or suffixing the ++ operator makes a difference at this point, since I do not know how the suffixed ++ is handled in a return statement. You might want to look up some specifications for this or just change it to the prefix-notation to be sure it is incremented (Who knows if the compiler things the suffix version is redundant and just throws it out, or something like that ^^).

- Michael.

Share this post


Link to post
Share on other sites
Quote:
I am not sure whether pre- or suffixing the ++ operator makes a difference at this point, since I do not know how the suffixed ++ is handled in a return statement. You might want to look up some specifications for this or just change it to the prefix-notation to be sure it is incremented (Who knows if the compiler things the suffix version is redundant and just throws it out, or something like that ^^).


This:


int * nextToken(int * currentToken) {
return currentToken++;
}


Always returns the same pointer that was passed in. The pointer is returned before the local scope copy is incremented. Because it's a post-increment operator. In other words the increment does nothing. There's no point incrementing a local variable and then returning from the function immediately.

This:


int * nextToken(int * currentToken) {
return ++currentToken;
}


Returns a pointer to a location one after that pointed to by the "currentToken" parameter. It's also the same as just doing: return currentToken + 1

Passing the pointer by reference, or passing a pointer to a pointer does not influence the return value. That is only necessary if you expect to call the function to increment the parameter, without using the function's return value.

Share this post


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

  • Advertisement