请帮我理解这个程序 [英] help me understand this program please

查看:60
本文介绍了请帮我理解这个程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h> 
    int main(void) { 
        int t[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, *p = t; 
    
        p += 2; 
        p += p[-1]; 
        printf("%d",*p); 
        getch(); 
    }



i了解p包含t数组中各个元素的地址但是算术我不能理解特别是p + = p [-1]并且在printf中表达将要打印的内容以及如何


i understand p contains the the addresses of respective elements in t array but the arithmetic i cant understand especially p+=p[-1] and in the printf expression what will be printed and how

推荐答案

所有解决方案都是正确的,但它们没有足够强调神奇的词: C中的指针算术

谷歌为它,你会得到你需要的所有答案。

要理解为什么你可以为数组元素类型的指针分配一个数组名称只要考虑一个数组名是指向其第0个元素的指针,所以:

All the solution are correct, but they don't highlight enough the magic word: "Pointers arithmetic in C".
Google for it and you'll get all answers you need.
To understand why you can assign an array name to a pointer to array element types just consider that an array name is a pointer to its 0th element, so:
int arr[10];    //An array of 10 integers
int *p = arr;   //p is a pointer to int initialized with arr
                //Is equivalent to p=&arr[0];



每次编译器看到数组名称时,都会创建一个指向数组第0个元素的指针,然后用它来访问数组。



指针算术允许求和和减法,以及操作数必须是数字。作为特殊情况,你可以减去2个指针,指向相同的类型,以得到一个数字作为结果。

C语言尝试实现指针的抽象处理,因为它们被管理为虚拟访问下一个每个增量或减量的值。当然,真实对象的大小跨越许多字节,因此对于某些类型,访问相邻值需要大于1的内存位移。这是由编译器默默处理的,它为每个增量添加所需的位移。




Each time the compiler sees an array name creates a pointer to 0th element of the array, then use it to access the array.

Pointers Arithmetic allow sum and subtraction, and the operand must be a number. As special case you can subtract 2 pointers, pointing to same type, to get a number as result.
C language try to implement an abstract handling for pointers in the sense that they are managed as virtually accessing next value each increment or decrement. Of course real objects have a size that spans over many bytes so that to access adjacent values requires a memory displacement bigger that 1 for some types. This is silently handled by the compiler, that adds for each increment the required displacement.
I.e.

 Type    Start Address  After increment
char      0x0021000      0x0021001    //Char   is 1 byte
int       0x0021000      0x0021004    //int    is 4 bytes
float     0x0021000      0x0021004    //float  is 4 bytes
double    0x0021000      0x0021008    //double is 8 bytes
....



指针算术运算可以描述为:


A pointer arithmetic operation can be described as:

address = (<type pointed> *)(((char *)p)+(number * sizeof(<type pointed>)))



如果指针p被转换为char指针以获得1字节寻址,那么我们添加我们的数字乘以指向类型的大小,而不是重铸指向我们的类型。

这适用于任何类型的类型,结构,联合等。在这种情况下,递增1我们将指向下一个结构。

减法与sum的工作方式相同,只有数字是负数。



减法的特殊情况两个指针中的整数指向两个地址之间的类型对象数。



你不能总和两个指针。这是非法的。



现在索引运算符[]相当于在用指针中的数字递增指针后检索指针指向的值:


Where the pointer p is casted to a char pointer to get 1 byte addressing, then we add our number multiplied by the size of the type pointed, than recast to point to our type.
This is valid for any kind of type, structures, unions, etc. In that case incrementing by one we will point to next structure.
Subtraction works the same way as sum, only the number is negative.

The special case of subtraction of two pointers gives the integral number of objects of type pointed to between the two addresses.

You cannot sum two pointers. It is illegal.

Now the index operator, [], is equivalent to retrieve of value pointed by pointer after incrementing the pointer by the number in braces:

p[n] ==> *(p + N)



这个操作符合我们之前已经说过的关于数组名称的数组或指针的使用(替换为带有指向其第0个元素的指针的编译器。



如果您理解这一点,您将理解您的代码......;)


And this operation is conform to use of an array or a pointer on the basis of what we already said before about an array name (replaced by compiler with a pointer to its 0th element).

If you understood this you will understand your code... ;)


如果我们看这里: http://c-faq.com/aryptr/aryptrequiv.html [ ^ ]

我们读到的尽管事实上,底层数组和指针是完全不同的,编译器不会应用数组下标operator [],这与数组和指针不同,毕竟。



If we look here: http://c-faq.com/aryptr/aryptrequiv.html[^]
we read "in spite of the fact that the underlying arrays and pointers are quite different, the compiler doesn't apply the array subscripting operator [] that differently to arrays and pointers, after all".

#include <stdio.h>
#include <conio.h>

  int main(void) { 

  int t[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

  int  *p = t;  // Assigns address of array t to p. This is also address of t[0].

  p += 2;  // Increment p by 2 - now points to t[2] == 3

  printf("%d\n", *p); 

  printf("%d\n", p[-1]);  // From link above p[-1] is the value -1 from where pointer currently points and thus is actually *(p+2-1) or t[1]  

   p += p[-1];  //  p is now t + 2 + 2 thus t[4]

   printf("%d\n", *p); 


   getch(); 

   return 0;
 }


请允许我逐行说明。



第1行:#include

此行缺少包含文件或头文件。我相信你需要包含stdio.h,因为使用了printf,而conio.h是因为使用了getch。



第2行:int main(void){

第3行:int t [10] = {1,2,3,4,5,6,7,8,9,10},* p = t;

t是一个整数数组,p是指向整数的指针。

在这一行中,指针p被赋值给数组t,因此它指向t数组中的第一个整数。变量t也可以用作整数的指针。



第4行:

第5行:p + = 2;

为指针p添加2个整数空格。现在,指针p指向数组t中的第三个整数。



第6行:p + = p [-1];

在C中,指针也可以用作数组。

p [0]与* p相同。

p [1]与*(p + 1)。

所以p [-1]与*(p - 1)相同

由于指针p指向第三个整数,所以指针( p - 1)指向第二个整数。 (p - 1)指向的值是2.

因此,p + = p [-1]与p + = 2相同,这意味着向指针p添加2。现在,指针p指向数组t中的第五个整数。



第7行:printf(%d,* p);

第8行:getch();

第9行:}

5将打印在此处。

a返回语句建议用于静默编​​译器警告。
Allow me to explain line by line.

Line #1: #include
This line missing a include file or header file. I believe you need to include stdio.h since printf is used, and conio.h since getch is used.

Line #2: int main(void) {
Line #3: int t[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, *p = t;
t is an integer array, and p is a pointer to integer.
In this line, pointer p is assigned to array t, so it points to first integer in t array. Variable t can be used as pointer to integer as well.

Line #4:
Line #5: p += 2;
Adding 2 integer spaces to the pointer p. Now, pointer p is pointing to third integer in array t.

Line #6: p += p[-1];
In C, pointer can also used as an array.
p[0] is same as *p.
p[1] is same as *(p + 1).
so p[-1] is same as *(p - 1)
Since pointer p is pointing to third integer, so pointer (p - 1) is pointing to second integer. The value pointed by (p - 1) is 2.
Therefore, p += p[-1] is same as p += 2, which mean adding 2 to the pointer p. Now, pointer p is pointing to fifth integer in array t.

Line #7: printf("%d",*p);
Line #8: getch();
Line #9: }
"5" will be printed here.
a return statement is recommended to silent compiler warning.


这篇关于请帮我理解这个程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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