![]() |
|
#1
|
|||
|
|||
Pointer arithmeticI have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,
If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be, 1 4 7 2 5 8 3 6 9 The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on. Here's what I have so far: CPP / C++ / C Code:
Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix? Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements. |
|
#2
|
||||
|
||||
|
Hello sanman10535,
Take a few minutes of your time to read this: http://www.gidforums.com/t-689.html. __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#3
|
|||
|
|||
|
I have to write the function vec_to_mat (int *v, int *mat, int n); v is a pointer to the beginning of an integer array (vector) with n x n elements stored in column-major representation (see below for explanation). The function should copy the elements from v to mat (which is a matrix of size n x n). From example,
If v is {1, 2, 3, 4, 5, 6, 7, 8, 9}, the value of mat should be, 1 4 7 2 5 8 3 6 9 The values 1, 2, and 3 go down the first column of matrix mat, and then 4, 5, 6 goes down the second column and so on. Here's what I have so far: CPP / C++ / C Code:
Also, why do I need to pass matrix[0] to the function vec_to_mat and not just matrix? Oh yeah, I have to use pointer arithmetic (not array subscripting) to visit the array elements. |
|
#4
|
|||
|
|||
|
Quote:
AHRRRR, forgot to add some more code |
|
#5
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#6
|
|||
|
|||
|
Quote:
You want to pass the address of matrix to the function (this a pointer to an int, since matrix is a (two-dimensional) array of int). Two ways of writing address of matrix: CPP / C++ / C Code:
or CPP / C++ / C Code:
Dave |
|
#7
|
|||
|
|||
|
yes dave is right, it's because your function vec_to_mat's second formal parameter is probably a single dimensional array: int mat1[]; if your second formal parameter was 2d array: int mat1[][3]; then you would have to pass it matrix and not just matrix[0]; for example
CPP / C++ / C Code:
just think of it as breaking off a row from your 2d matrix and passing it to your vec_to_mat function. you will not just be passing matrix[0] but you will also need to call vec_to_mat function and pass it matrix[1] and then matrix[2] and so on. you will need to call this function as many times as there are rows in the matrix, for each row. also to do pointer arithmetic, just assign a pointer to the first element of your array and or your array itself just keep incrementing it till you reach the end. for example: CPP / C++ / C Code:
Quote:
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Pointers-general confusion of pointer type. | warny_maelstrom | C Programming Language | 3 | 11-Mar-2004 22:26 |
| Pointer values changing unexpectedly | spudtheimpaler | C Programming Language | 11 | 04-Mar-2004 17:37 |
| modulus arithmetic | ewallo | C++ Forum | 3 | 23-Feb-2004 00:35 |
| storing a token pointer as a string | CoreLEx | C Programming Language | 1 | 07-Oct-2003 12:33 |
| convert long to pointer to char | realpopeye | C++ Forum | 2 | 26-Sep-2003 11:22 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The