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 300101120210111221221) 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. 这篇关于指针前/后增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 12:55