Question:
What is the pointer notation for multi-dimensional arrays. For example, for a single array, I know you can write it as *(p+i), where i can increase or decrease. How would I do this for a multi-dimensional array?
Thanks
Printable View
Question:
What is the pointer notation for multi-dimensional arrays. For example, for a single array, I know you can write it as *(p+i), where i can increase or decrease. How would I do this for a multi-dimensional array?
Thanks
for a specific example in my code:
Can someone show me how I would transfer a single array (name2[j]) to a multi-dimensional array (name[addr_temp][j]) ? How would I assign and declare the pointers for the multi-dimensional array pointer?Code:for(i=0; i< M-1; i++)
{
if(age[i][0] == 0)
{
addr_temp = i;
j=0;
do
{
name[addr_temp][j] = name2[j];
j++;
}
while(name2[j] != '\0');
break;
}
}
Remember that the farthest-to-the-right index changes fast, so adding one position changes the rightmost index. So name[a][b] lives at the a*(size of second dimension)+b location.
another way would be to set the pointer to the first element [row=0][col=0] of the 2D array as in p = name[0] and then use the pointer and an offset notation *(p + i)
well, I need the change the array accordingly. So if I had an array with a dimension like this:
array[10][15] (so it has 10 rows, and 15 columns)...
the way I would be able to access a specific row and all the columns would be this (say row 6): ?
a = array
a*(6) + i (where i will increment from 0-14)?
I'm not quite sure if I have the notation down...
if you wanted to access the last element of row 6 the notation is...
*(p + i) where the offset i = num_of_cols X row + num_of_cols
i = num_of_cols X row + num_of_cols
so if I had an array of array[10][15], and wanted to access last element of row 6, then it would be
*(p+i) where i = (15)(6) + 15 ?
I'm just trying to use a real example so I can make sure I understand it fully, thanks
Why don't you write a small example where you fill each row and colum with a different value (e.g. a multiplcation table), and then write another function to extract the data and see that you get it rigth. You could also write a 3 dimensional multiplication array (x * y * z), and see that you get that right.
--
Mats
but is the way that I am presenting it, correct?
Multidimensional arrays aren't stored as rows and columns...
Try it out as matsp adviced and if you run into problems you can always ask back here.