V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icebitch
V2EX  ›  C

float a[20], *p=a; 什么情况下不能用 p[i] ?

  •  
  •   icebitch · 2016-03-22 13:42:52 +08:00 · 2647 次点击
    这是一个创建于 2958 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    void main() {
    	float a[20], *p = a;
    	int n;
    	a[1] = 2.1;
    	printf("%f",p[1]);
    	puts("你想輸入幾個數(<=20)");
    	scanf_s("%d", &n);
    	for (int i = 0; i < n; i++)
    		scanf_s("%f",p+i);//為什麼這裡不能用 p[i]? 书上明明说可以~!~
    	for (int i = n-1; i > -1; i--)
    		printf("%f  ", *(p + i));
    	printf("\n");
    }
    
    3 条回复    2016-03-22 13:58:07 +08:00
    bearice
        1
    bearice  
       2016-03-22 13:49:42 +08:00   ❤️ 2
    p[i] == *(p+i)

    p+i == &p[i]
    icebitch
        2
    icebitch  
    OP
       2016-03-22 13:54:11 +08:00
    @bearice 谢谢, 掩面~~~~
    extreme
        3
    extreme  
       2016-03-22 13:58:07 +08:00   ❤️ 1
    p[i]等价于*(p+i)
    p+i 是一个指针,也就是地址, p[i]是一个具体的值,是 p+i 这个地址所指向的值。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1203 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 23:36 · PVG 07:36 · LAX 16:36 · JFK 19:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.