指针前/后增量 [英] Pointers pre/post increment

查看:81
本文介绍了指针前/后增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以完全解释以下代码吗?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *a, *s, i;

    a = s = (int *) malloc(4 * sizeof(int));

    for (i = 0; i < 4; i++)
    {
        *(a + i) = i * 10;
        printf(" %d ", *(a + i));
    }

    printf("\n");
    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    printf("\n");
    printf("%d\n", *a++);
    printf("%d\n", (*a)++);
    printf("%d\n", *a);
    printf("%d\n", *++a);
    printf("%d\n", ++*a);

    return 0;
}

输出:

0 10 20 30
0
10
11
20
21

0 
11
12
21
22

1)指针"s"如何打印值,其中仅分配了*(a+i) for循环中的值?

2)分配*(a+i)时,该值将准确存储在何处?

3)*s++(*s)++*++s++*s有什么区别?

4)为什么当我打印类似于s的指针时,值会增加1?

先谢谢了! :)

解决方案

1和2)指针指向(或可以说它是)某个内存位置的地址.由于分配了a = s = (int*) malloc(4 * sizeof(int));,因此as都具有相同的地址,或指向相同的存储位置.如果内存位置的内容有任何更改(例如,在代码中,您正在为分配的内存分配数字),则只要您具有正确的地址(a指向同一位置),您可以看到内容更改.

一个粗略的比喻是,您要房子(malloc),然后将房子的地址(a)退还给您.然后,您认为房子很丑陋,并且想要重新粉刷它(分配值:*(a + i) = i + 10),其他您告诉住址(s)的人也会看到您的房子已经粉刷过.

3)

*s++表示访问当前地址处的内容,而稍后会增加指针(地址).

反射回您的代码,它访问第一个元素,然后地址将指向第二个元素.

(*s)++表示访问当前地址的内容,而稍后则增加当前地址的内容.

反射回您的代码,它会在递增第二个元素之前获取它的内容.下一条打印语句显示第二个元素的内容已增加.

*++s表示递增当前地址,并在递增地址处访问内容.

反射回您的代码,它获取第三个元素的内容.

++*s表示在当前地址增加内容,并访问增加的内容.

反射回您的代码,它获取第三个元素的增量内容.

4) 如前面部分所述,如果通过一个指针修改内容,则如果具有相同的指针(地址),则将看到该内容.您实际上是在修改内存地址的内容(如3中所述),因此当您重复该过程时,您可能会看到修改的效果.

Can anyone please explain the following code completely?

#include<stdio.h>
#include<stdlib.h>

int main()
{
    int *a, *s, i;

    a = s = (int *) malloc(4 * sizeof(int));

    for (i = 0; i < 4; i++)
    {
        *(a + i) = i * 10;
        printf(" %d ", *(a + i));
    }

    printf("\n");
    printf("%d\n", *s++);
    printf("%d\n", (*s)++);
    printf("%d\n", *s);
    printf("%d\n", *++s);
    printf("%d\n", ++*s);
    printf("\n");
    printf("%d\n", *a++);
    printf("%d\n", (*a)++);
    printf("%d\n", *a);
    printf("%d\n", *++a);
    printf("%d\n", ++*a);

    return 0;
}

output:

0 10 20 30
0
10
11
20
21

0 
11
12
21
22

1) How pointer 's' is printing the values, where *(a+i) only been assigned the values in for loop?

2) Where does the value go exactly and stored when *(a+i) is assigned?

3) What's the difference between *s++, (*s)++, *++s, ++*s ?

4) Why the values are incremented by 1 when i print the pointer a similar to s?

Thanks in Advance ! :)

解决方案

1 and 2) Pointer points to (or you can say it is an address of) certain memory location. Since you assign a = s = (int*) malloc(4 * sizeof(int));, a and s both has the same address, or points to the same memory location. If anything changes to the content at the memory location (e.g. in your code, you are assigning numbers to the allocated memory), then as long as you have the right address (both a and s points to the same location), you can see the content changes.

A rough analogy is that, you ask for a house (malloc), and it gives you back the address of the house (a). Then you decide that the house is ugly, and you want to re-paint it (assign value: *(a + i) = i + 10), other people who you told the address to (s) will see that your house has been repainted.

3)

*s++ means access the content at the current address, and later increments the pointer (address).

Reflect back to your code, it accesses the first element, then the address will point to the second element.

(*s)++ means access the content at the current address, and later increments the content at the current address.

Reflect back to your code, it gets the content of the second element before incrementing it. The next print statement shows the content of the second element having been incremented.

*++s means increments the current address, and access the content at the incremented address.

Reflect back to your code, it gets the content of the third element.

++*s means increments the content at the current address, and access the incremented content.

Reflect back to your code, it gets the incremented content of the third element.

4) As explained in earlier part, if you modify the content via one pointer, you will see it if you have the same pointer (address). You are actually modifying the content of the memory addresses (as explained in 3), so you may see the effect of modification when you repeat the process.

这篇关于指针前/后增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆