Find the index address in an array in C

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

I got:

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

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

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

+5
source share
3 answers

Just print the address of the variable you will see !:

#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 are right!

And how do you see your right! it's 334

+3
source

Your math is correct. You can check by subtracting the two addresses, but do not forget that the pointer arithmetic recognizes the font size, so you need to specify the addresses in char whose size is equal to the byte:

 ( char* )&a[1][1][1][1] - ( char* )&a[0][0][0][0] 

which gives a difference in bytes. Then just add the start address and you have the answer.

+3
source

Something like this is simple enough to check (a) :

 #include <stdio.h> int main (void) { int a[2][3][4][5]; // Ignore incorrect format specifiers for now. printf ("%d\n", sizeof(int)); printf ("%d\n", &(a[0][0][0][0])); printf ("%d\n", &(a[1][1][1][1])); printf ("%d\n", (int)&(a[1][1][1][1]) - (int)&(a[0][0][0][0]) + 1000); return 0; } 

and the conclusion of this:

 4 2665056 2665400 1344 

Note the conversion of pointers to int values ​​in this printf final. Without this, 1000 will scale as int * , indicating the wrong value.

So yes, in the bottom line, your reasoning is correct.


(a) This is not always the case, since some aspects of the C language may differ in different implementations (the behavior specified in the implementation) or in some way they want (undefined behavior).

Fortunately, array layout is specifically specified by the standard, in C11 6.5.2.1 Array subscripting :

2 / A postfix expression followed by an expression in square brackets [] is the indexed designation of an element of an array object. The definition of the index operator [] is that E1[E2] identical (*((E1)+(E2))) . Due to conversion rules that apply to the binary + operator, if E1 is an array object (equivalent to a pointer to the source element of an array object) and E2 is an integer, E1[E2] stands for E2-th element of E1 (counting from zero).

3 / The operators of subsequent indices denote an element of a multidimensional array object. If E is an n-dimensional array (n> = 2) with dimensions i * j * ... * k , then E (used as different from lvalue) is converted to a pointer to an (n - 1) -dimensional array with dimensions j * ... * k . If the unary * operator is applied to this pointer explicitly or implicitly as a result of the signature, the result is a reference (n - 1) -dimensional array, which itself is converted to a pointer if it is used as a value other than lvalue.This implies that the arrays are stored in lowercase order (the last index changes faster).

0
source

Source: https://habr.com/ru/post/1206760/


All Articles