c++ - how can I assign value into 2-D array by using their address? -
i'm going rewrite program using pointer & dynamic array
but 2-d array part, here question
this original code:
(int index = 0; index < 12; index++) { sorted[index][0] = sum[index]; sorted[index][1] = index+1; }
and rewrite :
for (int index = 0; index < 12; index++) { *(*sorted+index) = *(sum+index); *((*sorted+index)+1) = index + 1; }
i have tried , problem occur in first part of assign *(*sorted+index)
, *((*sorted+index)+1)
what problem going on? there no error code, descrition just:
exception thrown @ 0x00f47379 in ass2 q3.exe: 0xc0000005: access violation writing location 0xce13e05c. unhandled exception @ 0x00f47379 in ass2 q3.exe: 0xc0000005: access violation writing location 0xce13e05c.
you have problem in these 2 lines:
*(*sorted+index) = *(sum+index); *((*sorted+index)+1) = index + 1;
you need move sorted
pointer index
offset, , should write (sorted + index)
can value stored in address *(sorted + index)
first element *(*(sorted + index))
. same thing must applied second line too, 2 lines should be:
*(*(sorted+index)) = *(sum+index); *(*(sorted+index)+1) = index + 1;
i suggest read more pointer arithmetic.
Comments
Post a Comment