开发者

C pointer: *p++ and p++ difference

开发者 https://www.devze.com 2023-04-12 23:00 出处:网络
I have just started learning C. void function(char *str1, char *str2) { printf(\"%p\\n\", str1); printf(\"%p\\n\", str2);

I have just started learning C.

void function(char *str1, char *str2) {
    printf("%p\n", str1);
    printf("%p\n", str2);
    str1++; //*str++;
    str2++; //*str++;
    printf("%p\n", str1);
    printf("%p\n", str2);
}

int main(int argc, char *argv[]) {
    function(argv[1], argv[2]);
    return 0;
}

if argv[1] is "basic" argv[2] is "program";

when I replace the code with comment part, it would give the same result, which is "a" "r"; but if I compile it with flag -Werror, it would give "warning : value computed is not used"

What is difference between str1++ and *str1++;?

what is the code if i want to increment the character it points to

for the line : function(argv[1], argv[2]); in here the argv[1] and agrv[2] I pass to the function, are they pointers or they ar开发者_如何学Ce the array? what is *argv[1] and *argv[2]


what is difference between str1++ and *str1++

They have exactly the same side-effects (increment the pointer).

They heve, however, different values. The first evaluates to the value of the pointer before incrementing, the second evaluates to data it points to. You don't use the value anyway, which is what your compiler is noticing you about.

function(argv[1], argv[2]); in here the argv[1] and agrv[2] I pass to the function, are they pointers or they are the array?

They are pointers to char, ie. strings. Particularly the 1st and 2nd command line parameters.


Every (sub-)expression in C (with a few exceptions) has a value and possibly a side-effect.

Expressions can be used just for the value, as in 42 below.

int a = 42;

They can also be used just for the side-effect, as below

printf("Hello, world!\n");

And, of course, they can be used for both the value and the side-effect (the getchar below):

while ((ch = getchar()) != EOF) /* void */;

Your compiler is just complaining that you computed a value and did not use it. The compiler knows not to complain for usual construct like printf() but not in the specific case of *str++


The expression *str++ has value *str and side-effect of incrementing str. As you don't use the value, it is the same as str++ (with value str and same side-effect and usual construct that the compiler doesn't complain about).


The difference is best illustrated by modifying your function some. Bear in mind that ++x differs from x++ only in return value:

void function(char *str1, char *str2) 
{
    printf("%s\n", str1);
    printf("%s\n", str2);
    printf("%s\n", ++str1);
    printf("%s\n", ++str2);
}

calling function as function("hello", "world") would print

 hello
 world
 ello
 orld

Now, let's modify function to use *++str instead of ++str

void function(char *str1, char *str2) 
{
    printf("%s\n", str1);
    printf("%s\n", str2);
    printf("%c\n", *++str1);
    printf("%c\n", *++str2);
}

The output of the same call is now:

hello
world
e
o

The * simply dereferences whatever comes after it, in this case the beginning of a string.


Pointers are fixed sizes, arrays are not. sizeof(char*) == sizeof(int) == sizeof(float**) and so on. The idea is you're passing a fixed-size pointer to this array of characters versus copying the entire thing. You get the value of the character array by dereferencing with *, but that really isn't useful with strings most of the time. Dynamic strings are typically left as pointers so they can account for arbitrary size. I do not know of any reason why you would want to increment a pointer, but incrementing any letter in the string would make sense.

Also, I tend to use pre-increment. It is faster by every benchmark I have seen.

0

精彩评论

暂无评论...
验证码 换一张
取 消