在C使用指针 [英] Using pointers in C
本文介绍了在C使用指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
的#include<&stdio.h中GT;INT主要(无效)
{
诠释一个[5] = {1,2,3,4,5};
为int * ptr的=为(int *)(&放大器;一个+ 1);
的printf(%D \\ n,*(A + 1)*(PTR-1));
返回0;
}
输出:
2,5
我不明白如何使用 *(PTR-1)
评估为5(正确的输出)。但是,当我手动
也有人1.我的理解是 *(PTR-1)
将得到评估,以<code> *(安培A + 1-1)这将是 *(&安培; A)。
为1
请帮我理解这个概念。
解决方案
为int * PTR =(INT *)(安培A + 1);
品牌&安培A + 1
,使&安培A +的sizeof(A)
为在
是 INT [5]
这使得 PTR
来点实际的类型 A [5]
(无效/超出定义的限制)
(PTR - 1)
指出从而指向 A [4]
和 * (PTR - 1)。
将打印 5
#include <stdio.h>
int main(void)
{
int a[5]={1,2,3,4,5};
int *ptr=(int*)(&a+1);
printf("%d %d\n",*(a+1),*(ptr-1));
return 0;
}
Output:
2,5
I could not understand how the *(ptr-1)
evaluated to 5 (correct output). But when I manually
did it was 1. My understanding is *(ptr-1)
will get evaluated to *(&a+1-1)
which would be
*(&a)
which is 1.
Please help me to understand this concept.
解决方案
int *ptr=(int*)(&a+1);
makes &a + 1
makes &a + sizeof (a)
as a
is type of int [5]
which makes ptr
to point to actually a[5]
(invalid/beyond the defined limit)
(ptr - 1)
points thus points to a[4]
and *(ptr - 1)
will print 5
.
这篇关于在C使用指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文