Why ++str and str+1 is working and str++ isn't?
Asked Answered
S

4

6

I know that here are some explanations about the difference between p++, ++p and p+1 but I couldn't understand it clearly yet, especially when it's not working with that function:

void replace(char * str, char c1, char c2){

    if (*str == '\0') {
        return;
    }else if (*str == c1) {
        printf("%c", c2);
    }
    else {
        printf("%c", *str);
    }

    replace(++str, c1, c2);
}

When I do replace(++str, c1, c2); or replace(str+1, c1, c2); it works, but replace(str++, c1, c2); doesn't. Why?

Snotty answered 10/5, 2020 at 0:10 Comment(1)
Besides the question of which increment works, there is no point in incrementing str at all, since str is no longer used after replace. The natural way to write it would be simply replace(str + 1, c1, c2).Undercroft
O
7

replace(str++, c1, c2); means:

replace(str, c1, c2);
str+=1;

while replace(++str, c1, c2); means:

str+=1;
replace(str, c1, c2);
Osterhus answered 10/5, 2020 at 0:13 Comment(0)
A
4

The expression str++ yields the value before incrementing its operand. So you are calling the function with the same value of str.

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it)

You may consider this function call

replace(str++, c1, c2);

like

replace(str, c1, c2);
str += 1;

In two other calls

replace(++str, c1, c2);

and

replace(str+1, c1, c2);

you are passing incremented value of the string pointer.

Pay attention to that your function does not replace characters in the source string. It just outputs the string replacing characters in the output. The source string is not changed

In this case the first function parameter should be declared with the qualifier const.

void replace(const char * str, char c1, char c2);

If you want to change the source string then the function can look as it is shown in the demonstrative program below.

#include <stdio.h>

char * replace( char *s, char c1, char c2 )
{
    if ( *s && *s == c1 ) *s = c2;

    if ( *s ) replace( s + 1, c1, c2 );

    return s;
}

int main(void) 
{
    char s[] = "Lucy is learning c";

    puts( replace( s, 'c', 'C' ) );

    return 0;
}

The program output is

LuCy is learning C
Amarette answered 10/5, 2020 at 0:13 Comment(0)
C
2

Both the prefix ++ operator and the postfix ++ operator increment the argument. The difference is in the result of the expression.

The prefix ++ evaluates to the value of the argument after incrementing, while the postfix ++ evaluates to the value of the argument before incrementing.

For example:

int i = 1;
printf("i=%d\n", ++i);   // i == 2, prints 2
printf("i=%d\n", i++);   // i == 3, prints 2
Carrycarryall answered 10/5, 2020 at 0:14 Comment(0)
R
2

++str and str+1 makes the the pointer point to the next byte, while the str++ passes the current location of the pointer as an argument.

Reconstructionist answered 10/5, 2020 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.