+ 11

[SOLVED] Need help understanding array subscript operator

Let's assume a one dimension int array int array[5] = {1, 2, 3, 4, 5}; To get an element, we can use subscript operator: `array[0]` or `array[1]` Or dereference operator: `*array`, or `*(array + 1)`. And now for the question; 1. How do I obtain value of an element from a two (or more) dimension array by using dereference operator?. 2. For a 2D array case, is swapping of indexes possible using dereference operator? for example, I understand we can use subscript operators such as: `array[row][col]` And we can swap the index such that: `array[col][row]` Is index swapping possible by using dereference operator? if it is, how? is it considered a good practice? * I tried searching for this topic but got overwhelmed by irrelevant results, I'm sorry if this had been discussed before (duplicate links are welcome). Big Thanks for any enlightenment, in advance.

16th May 2019, 7:28 PM
Ipang
7 Respostas
+ 5
~ swim ~ I was just exercising with an assignment "Magic Square" and found myself having to swap the index during calculation. In the code below I still use the subscript operator inside `is_square` function. It is the reason why I asked how to do that (swapping index) using the dereference operator : ) Big Thanks for stopping by 👍 https://code.sololearn.com/cAjmtQLum60D/?ref=app
16th May 2019, 8:44 PM
Ipang
+ 5
Alright mate, now I see the reason clearly. Big Thanks for the notes 👍
17th May 2019, 7:42 AM
Ipang
+ 4
Paul I noticed a difference in asterisk position in your answer and the code, does that make any difference if I may ask?
16th May 2019, 8:39 PM
Ipang
+ 4
~ swim ~ Yeah I get that too sometimes, worst part if I forget to copy the answer and had to write from scratch again 😁 I was a bit confused of the two, but now it's fine, I understand it now. I just need more practice on this to get used to it, now that I know the formula 👍 Big Thanks again for clarification brother 😁
16th May 2019, 10:24 PM
Ipang
+ 4
Big Thanks Bennett Post for extended response 👍 Can I have "unformatted" version of the reason on page 1/2? I can't read that styled text well from here 😁
17th May 2019, 7:20 AM
Ipang
+ 3
Paul So this is how I read your example: *table -> row 0 *(table + 1) -> row 1 *(table + 2) -> row 2 ... *(*(table)) -> 1st column of row 0 *(*(table) + 1) -> 2nd column of row 0 *(*(table + 1)) -> 1st column of row 1 *(*(table + 1) + 1) -> 2nd column of row 1 I hope I get it right : ) Thank you very much for your help! I will study the example from the code. P.S. Please correct me if I still not getting the example right : )
16th May 2019, 8:29 PM
Ipang
+ 2
A pointer is simply an address in memory. int array[4]; array is the address of the first int in memory. For multidimensional array, it's not more complex. int table[4][4]; table is the address of the pointer to the array 0 of the table, thus *table is an array. If you want the second value of this array, you can write *((*table)+1) If you want the second array, it is the same as your description *(table +1). Thus, the third element of the second column is *(*(table+1)+2) Example: https://code.sololearn.com/cFHh1U3230G2/?ref=app 2 array[row][col] is not equal to array[col][row] if row is different from col Edit: sorry, I made a mistake in my above example(the code is fine) *table + 1 is the address of the second element of the first array, while *(table + 1) is the address of the the first element of the second array So for your example, the second star should be before the asterix
16th May 2019, 7:54 PM
Paul