Post 9frQLqONSv364MXo5g by olligobber@mathstodon.xyz
(DIR) More posts by olligobber@mathstodon.xyz
(DIR) Post #9frQGqOg1J5kdWIfWC by Silverrpent@mathstodon.xyz
2019-02-12T09:40:49Z
0 likes, 2 repeats
c++ being likeint c[3] = {1,5,8};cout << 1[c];This bit of code writes "5" on the consoleWhat
(DIR) Post #9frQLqONSv364MXo5g by olligobber@mathstodon.xyz
2019-02-12T12:04:08Z
0 likes, 0 repeats
@Silverrpent wat
(DIR) Post #9frQLqpfpQspR1JbdY by Silverrpent@mathstodon.xyz
2019-02-12T20:08:35Z
0 likes, 0 repeats
@olligobber So turns out that it is because arrays just don't exist, the name of the array is instead a name for a pointer to the place the array starts, and when you write in... sharp brackets (too lazy to look up the English name for it), it just adds to the pointer. So, c[5] translates to *(c+5) -- and 5[c] translates to *(5+c). And they just mean the same things since commutative(As nice people like @axiom and @JordiGH said -- thank you guys, and feel free to correct me if I said stupid)
(DIR) Post #9frQLrCiRlJaaU60YK by JordiGH@mathstodon.xyz
2019-02-12T20:22:18Z
0 likes, 2 repeats
@Silverrpent @olligobber @axiom Almost.Arrays do exist.The type of c is int[3], which is a different type than int*. A variable declared int[3] in one file and referred to as int* in another file will fail to be the same variable.The only thing is that in C (and therefore C++), arrays decay into pointers at the merest insinuation.Seehttp://c-faq.com/aryptr/aryptr1.htmlandhttp://c-faq.com/aryptr/aryptrparam.html
(DIR) Post #9fs39aTEjSoxQVxnhw by tuxcrafting@pleroma.tuxcrafting.cf
2019-02-15T20:58:23.490022Z
0 likes, 0 repeats
@Silverrpent when you realize that a[b] = *(a+b*sizeof(typeof(a)) it makes sense
(DIR) Post #9g5DZ1Yf0n7cCw8zTc by axiom@mathstodon.xyz
2019-02-21T22:01:02Z
1 likes, 0 repeats
@tuxcrafting @Silverrpent not quite, since the scaling is implied in C whenever you add an int to a pointer. The actual equivalence is:a[b] = *(a + b) =*((A *) (((void *) a) + b*sizeof(A))))