Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

C语言二维数组索引的问题?

我有一个二维数组,data[512][512], 现在有下面两种索引,为什么得到的值不一样?有没有比较懂的老哥?

data[1][1]
*(&data[0][0]+513)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你的数组定义和运行环境是什么,我测试了下两个是一样的啊

[root@db /home/clion]# cat main.c
#include <stdio.h>

void main(){
    short data[512][512];
    
    data[1][1] = 111;
    
    printf("data[1][1] is %d.
", data[1][1]);
    printf("*(&data[0][0]+513) is %d.
", *(&data[0][0]+513));

    printf("address1 is %p
", &data[1][1]);
    printf("address2 is %p
", &data[0][0] + 513);
}
[root@db /home/clion]# ./main
data[1][1] is 111.
*(&data[0][0]+513) is 111.
address1 is 0x7ffc0c9ff2a2
address2 is 0x7ffc0c9ff2a2
[root@db /home/clion]# 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...