在C中的数组中找到索引的地址 [英] Find the address of an index in an array in C

查看:90
本文介绍了在C中的数组中找到索引的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定C中数组的定义:int a [2] [3] [4] [5],而a [0] [0] [0] [0]的地址为1000,则该地址为假设一个int占用4个字节,则a [1] [1] [1] [1]的大小.

Given the definition of an array in C: int a[2][3][4][5], and the address of a[0][0][0][0] is 1000 what is the address of a[1][1][1][1], assuming an int occupies 4 bytes.

我知道了:

(3 * 4 * 5 * 4bytes)+(4 * 5 * 4bytes)+(5 * 4bytes)+ 4bytes = 344

(3*4*5 * 4bytes) + (4*5 * 4bytes) + (5* 4bytes) + 4bytes = 344

344 + 1000 = 1344 a [1] [1] [1] [1]的位置

344 + 1000 = 1344 Location of a[1][1][1][1]

但是我不知道我是否正确.但是我的数学对我来说似乎是正确的.

but I have no idea if I'm right. But my math seemed sound to me.

推荐答案

只要打印出变量的地址,就可以看到它!:

Just print the adress of the variable any you will see it!:

#include <stdio.h>  

int main() {

    int a[2][3][4][5];

    printf ("Size of int %d\n", sizeof(int));

    printf("Adress of the frist element \t%p\n", &a[0][0][0][0]);
    printf("Adress of x element \t\t%p\n", &a[1][1][1][1]);

    printf ("In decimal: \t\t\t%d\n", &(a[0][0][0][0]));
    printf ("In decimal: \t\t\t%d\n", &(a[1][1][1][1]));

    printf("Difference between the adresses %d", (char *)&a[1][1][1][1] - (char *)&a[0][0][0][0]);




    return 0;

}

之后,您可以检查您是否在正确的位置!

After that you can check if you where right!

如您所见,您的权利!它是 334

And as you see your right! it's 334

这篇关于在C中的数组中找到索引的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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