Click here to Skip to main content
15,891,136 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralC question: passing an 2D array for modifying Pin
bouli7-Aug-05 13:50
bouli7-Aug-05 13:50 
GeneralRe: C question: passing an 2D array for modifying Pin
Christian Graus7-Aug-05 13:53
protectorChristian Graus7-Aug-05 13:53 
GeneralRe: C question: passing an 2D array for modifying Pin
bouli7-Aug-05 13:57
bouli7-Aug-05 13:57 
GeneralRe: C question: passing an 2D array for modifying Pin
Christian Graus7-Aug-05 14:06
protectorChristian Graus7-Aug-05 14:06 
GeneralRe: C question: passing an 2D array for modifying Pin
bouli7-Aug-05 14:09
bouli7-Aug-05 14:09 
GeneralRe: C question: passing an 2D array for modifying Pin
Jose Lamas Rios7-Aug-05 16:55
Jose Lamas Rios7-Aug-05 16:55 
GeneralRe: C question: passing an 2D array for modifying Pin
Christian Graus7-Aug-05 16:57
protectorChristian Graus7-Aug-05 16:57 
GeneralRe: C question: passing an 2D array for modifying Pin
Jose Lamas Rios7-Aug-05 18:46
Jose Lamas Rios7-Aug-05 18:46 
Christian Graus wrote:
It works if you specify the array bounds ? Cool.

It does work Smile | :)

For an array of n dimensions you need to specify at least (n-1) bounds, because otherwise the compiler has no way to calc the offset for a given element. For example, if you have int matrix[3][5] (3 rows, 5 columns), then the element at matrix[i][j] (row i, column j), can be computed as *(matrix+(i*5)+j), that is, increment the matrix pointer so as to skip i rows of 5 elements each, then skip j elements, and then dereference the resulting pointer.

As can be seen in the example above, the number of rows isn't strictly needed, though (just like the number of elements isn't actually needed for an array of only one dimension). In fact one could even write a function that received as its argument a matrix of unbounded rows and n columns.
void test(int matrix[][5])
{
   matrix[2][3] = 1;
}
 
void main()
{
   int matrix[3][5];
   test(matrix);
}

If on the other hand the test function received the matrix as an int**, there'd be no way to know how many elements to skip for each row. The test function would compile, but the main function not, because int[3][5] is not the same as int**, the latter being a pointer to a pointer to an int [1]. In fact if we changed the test function to
void test(int** matrix)
{
   matrix[2][3] = 1;
}

The expression matrix[2][3] would compile to something entirely different than before. With matrix defined as int** an expression matrix[i][j] would translate to *((*(map+i))+j), that is in the example, incrementing the matrix pointer by 2, dereferencing the result, using that value as a pointer, incrementing it by 3 and then dereferencing that! To keep that working as before, we'd need to also alter the main function as follows:
void main()
{
   int matrix[3][5];
   int* rows[3];
   for (int i = 0; i < 3; i++)
      rows[i] = &matrix[i][0];
   test(rows);
}

There's still one additional correct way of writing the code, which can be used if one doesn't want test to be restricted to a particular number of columns:
void test(int* matrix, int nCols)
{
   matrix[2*nCols+3] = 1;
}
 
void main()
{
   int matrix[3][5];
   test(matrix[0],5);
}


[1] Depending on how it's used, it can also be seen as a pointer to an array of pointers to ints, or as a pointer to a pointer to an array of ints, or as a pointer to an array of pointers to arrays of ints.


--
jlr
http://jlamas.blogspot.com/[^]
GeneralRe: C question: passing an 2D array for modifying Pin
bouli8-Aug-05 0:28
bouli8-Aug-05 0:28 
GeneralRe: C question: passing an 2D array for modifying Pin
sunit57-Aug-05 18:21
sunit57-Aug-05 18:21 
GeneralRe: C question: passing an 2D array for modifying Pin
David Crow8-Aug-05 8:34
David Crow8-Aug-05 8:34 
GeneralWM_CHAR and eating keystrokes Pin
Luther Baker7-Aug-05 10:59
Luther Baker7-Aug-05 10:59 
GeneralRe: WM_CHAR and eating keystrokes Pin
PJ Arends7-Aug-05 12:09
professionalPJ Arends7-Aug-05 12:09 
GeneralRe: WM_CHAR and eating keystrokes Pin
Luther Baker7-Aug-05 17:39
Luther Baker7-Aug-05 17:39 
GeneralRe: WM_CHAR and eating keystrokes Pin
Luther Baker7-Aug-05 18:24
Luther Baker7-Aug-05 18:24 
Generalvector doesn't work - won't load Pin
Larry Mills Sr7-Aug-05 9:34
Larry Mills Sr7-Aug-05 9:34 
GeneralRe: vector doesn't work - won't load Pin
Shog97-Aug-05 12:03
sitebuilderShog97-Aug-05 12:03 
GeneralRe: vector doesn't work - won't load Pin
Larry Mills Sr8-Aug-05 15:45
Larry Mills Sr8-Aug-05 15:45 
GeneralRe: vector doesn't work - won't load Pin
Christian Graus7-Aug-05 13:52
protectorChristian Graus7-Aug-05 13:52 
GeneralRe: vector doesn't work - won't load Pin
Larry Mills Sr8-Aug-05 15:32
Larry Mills Sr8-Aug-05 15:32 
Generalusing namespace in a c++ dll Pin
MihaiChioariu7-Aug-05 9:33
MihaiChioariu7-Aug-05 9:33 
GeneralBoost + regex Pin
Jack Puppy7-Aug-05 7:47
Jack Puppy7-Aug-05 7:47 
GeneralSNMP: Urgent help needed Pin
Mukhan7-Aug-05 6:27
Mukhan7-Aug-05 6:27 
GeneralRegarding thumb recognition Pin
Pearlking7-Aug-05 4:40
Pearlking7-Aug-05 4:40 
GeneralRe: Regarding thumb recognition Pin
Maximilien7-Aug-05 5:16
Maximilien7-Aug-05 5:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.