I’m trying to learn how to figure out addresses in C. For the code below, assuming it’s compiled on a 32-bit little endian machine.
struct {
int n;
char c;
} A[10][10];
Say the address of A[0][0] is 1000(decimal), what would the address of A[3][7] be? Any help is appreciated!
C is row-major ordered, which means that the left-most index is computed first. Thus:
To find the column, we just add the remaining index:
Note that this has nothing to do with big-endian vs little-endian architecture. That’s just the location of bytes within a word, whereas you want the location of structs within an array.
Also,
sizeof(your_struct)isn’t guaranteed to besizeof(n) + sizeof(c)because the compiler could pad your struct.Lastly, the 32-bit nature of your machine means that the size of the memory-address register is 32 bits. (Or to put it another way,
sizeof(void*)==32). It’s an indication of how much memory your processor can actually assign an address to. That is a separate issue from the size of data types in C.