Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API

From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
cprn15902yWhen you declare an array as `ARR[15][20]` it just means there's 15*20*4 = 1200 bytes reserved for ARR in memory so if [10][5] is at 35000 memory offset then [0][0] is at 35000 - 10*5*4 = 35000 - 200 = 34800 offset and [5][15] is at 34800 + 5*15*4 = 34800 + 300 = 35100.
By the way `ARR[5][15]` can also be accessed as `ARR[75]` and `ARR[10][5]` can be accessed as `ARR[50]` - there's no difference in C-like languages. Array dimensions are solely contractual, offsets are the same. -
korrat6792y@cprn your index calculations are off. You cannot simply multiply the two indices together. Otherwise [5][10] and [10][5] would be the same element.
-
@korrat he'll probably just provide wrong solutions so people will provide the correct one.
But curious to see that his 0 offset is wildly different from mine -
bad-frog6182ythe correct answer is:
#include <stdio.h>
int main()
{
long long arr[15][20];
fprintf(stderr, "delta [5][15] : %llu\n", (unsigned long long)(&(arr[5][15]) - &(arr[0][0])));
fprintf(stderr, "delta [10][5] : %llu\n", (unsigned long long)(&(arr[10][5]) - &(arr[0][0])));
}
-> you look under the skirt -
bad-frog6182y@cprn
its (index_x * size_y) + index_y = index
you extract coordinates by doing
index / size_y = index_x
index % size_y = index_y -
cprn15902y@bad-frog Well, obviously, cynide108 did not know that and how is he going to learn if not by public humiliation? 🤷♂️ But now you've gave him the answer on a silver platter depriving him of this perfectly good opportunity for proper education.
The real answer is: it depends. There are two ways of storing arrays in memory - row major and column major. For row major it is like you said but for column major it's the other way around. -
bad-frog6182y@cprn took it as granted since c like are the languages where the question makes most sense.
afaik in all that goes higher level your arrays become more and more wilder.
and i dont know of any where you would specify in what order you input the indexes. -
If byte addressed,
10,5 is [(9*20)+5]*4 = 740 bytes from the start.
5,15 is [(4*20)+15]*4 = 380 bytes from the start.
So 5,15 will have address 34,640.
If 4 bytes (word) addressed:
10,5 is [(9*20)+5] = 185 words from the start.
5,15 is [(4*20)+15] = 95 words from the start.
So 5,15 will have address 34,910.
ARR [15][20] is a two - dimensional array, which is stored in the memory along the row with each of its elements occupying 4 bytes. Find the address of the element ARR [5][15], if the element ARR [10][5] is stored at the memory location 35000.
question