2

I'm converting a C prog that parses a text string and produces human readable o/p.

Got it nearly done, but having a problem understanding the difference in

*char_ptr++   

and
char_ptr-- in

 token[i++] = c = toupper(*char_ptr++);
 if (c == '\0')
   {
     char_ptr--;
     return( 0 );
   }

Am I correct in thinking that *char_ptr++ will effectivley point to the next char in the 'string'? If so, what does char_ptr-- do?

Thanks.

5 Answers 5

3

Regard *char_ptr++ as:

char tmp = *char_ptr;
char_ptr++;
c = toupper(tmp);

So it effectively fetches the current character, and advances the pointer by one. Because the ++ operator has a higher precedence than the unary *, such an expression is evaluated in the order *(char_ptr++).

The incrementation is applied first, but since the postfix ++ operator returns the result prior to the manipulation, the * dereferencing operator is applied on the old address.

char_ptr-- simply decreases the pointer by one.

Sign up to request clarification or add additional context in comments.

12 Comments

Ah - so char_ptr-- is effectively *char_ptr--
No, the first means n--. The second means arr[n--]
Hmm - that's what confuses me :) Given a string, successive calls to *char_ptr++ will give the next character. I'd expect *char_ptr-- to give the previous. In which case, what does char_ptr-- do?
make a later call to *char_ptr return the previous.
Essentially ptr++ or ptr-- will change the value of ptr[0].
|
1

In the same way as ++ increments the pointer and points to the next character, -- decrements it, pointing to the previous character. In this case, it puts it back to the last real character from the terminating null.

Comments

1

Because you're converting to java, you're going to have to remove the pointers, Java doesn't support them.

token[i++] = c = toupper(*char_ptr++);
if (c == '\0')
{
    char_ptr--;
    return( 0 );
}

Likely had some sort of declaration above it saying:

char* char_ptr = some_array;

Instead, that'll be

int pos = 0;

And the above code becomes:

token[i++] = c = toupper(some_array[pos++]);
if (c == '\0')
{
    pos--;
    return( 0 );
}

4 Comments

What's the problem? How is the program behaving?
Array out of bounds. I'll get there eventually!
Are Java strings null-terminated? Arrays in java have a .length field, so they might not be.
I've been initialisng the char[] with 0's and creating the strings in a loop. Fixed that prob now anyway. Evere onwards!
0

Yes, you are correct. It's a way of "peeking" ahead into the "string" and then returning to where you were if the string was at it's end.

Comments

0

This question is solved by precedence rules. The expression

*char_ptr++

is resolved to

*(char_ptr++)

meaning "The character after the character that ptr is pointing to". So when c equals a '\0' (zero), the pointer gets decremented, so it points to the previous character. It practically decrements to the previous character whenever there's a 0 encountered, so that char_ptr points to whatever it pointed to during the last run of the block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.