Copying From Single Dimension Array To Multiple Dimension Array
-
I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }
-
I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }
Are trying to transfer content of arr to arr3, right? But I am not able to see any memory allocation for arr3... You have to allocate some memory.. The way you are doing wont be safe in terms of memory pointer usage.. You can do something like this...
case 1: assigning direct pointer of arr to arr3
arr3 = new int[SIZE];
/* for loop with i*/
arr3[i] = &(arr[UPTO_SIZE])case 2: copyinh content of arr to arr3
arr3 = new int[SIZE];
/* for loop upto size */
arr3[i] = new int[size2]/* copy logic from arr to arr3*/
I hope you can add proper code in this logic... As we cannot complete your assignment... :D
-
Are trying to transfer content of arr to arr3, right? But I am not able to see any memory allocation for arr3... You have to allocate some memory.. The way you are doing wont be safe in terms of memory pointer usage.. You can do something like this...
case 1: assigning direct pointer of arr to arr3
arr3 = new int[SIZE];
/* for loop with i*/
arr3[i] = &(arr[UPTO_SIZE])case 2: copyinh content of arr to arr3
arr3 = new int[SIZE];
/* for loop upto size */
arr3[i] = new int[size2]/* copy logic from arr to arr3*/
I hope you can add proper code in this logic... As we cannot complete your assignment... :D
chevu, Thank you for your reply. I am not taking a class but doing self study to learn C. I will take your solution and walk through the logic to understand. Thanks again. Mike
-
chevu, Thank you for your reply. I am not taking a class but doing self study to learn C. I will take your solution and walk through the logic to understand. Thanks again. Mike
And ya if you don't want to new memory allocation you can directly assign address of arr to arr3, but for that in your function your have to send address of arr3 pointer i mean &arr3 (which you have declared as int** arr3), and in function you can assign address of arr to arr3....
-
I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }
-
I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }
For one, transarr() does not get all the information it needs: You have a single, one-dimensional array with 5 elements, and try to transfer it into another array with ROWS rows. But you do not give an indication about the width of those rows in the parameters. The width is only given through the global symbol SIZE2. Pass SIZE2 as a parameter instead, so transarr() does not depend on a global value and can be reused for different cases. Second, in your main program you define a two-dimensional array arr2, but you do not use it. Instead you pass another variable, arr3 to transarr - that doesn't make sense. I think your intention was to copy the members of arr into arr2, no? If so, you do not need the variable arr3, you can pass arr2 into the call of transarr(), you don't even need to change the function declaration for that. Third, the purpose of transarr is somewhat ... unusual: In your code you copy the original array into each row, creating multiple copies of that array. Is that your intention? Also, if SIZE2 happens to be greater than SIZE, then your copy loop will run into undefined memory as it will read beyond arr. Of course at the moment nothing happens, but if you get your program to run and later change SIZE or SIZE2, you might end up with random values in your resulting array and not know where they originate from.
-
I am currently trying to copy the contents from a single dimension array to a multiple dimension array using pointer notation. I though am having problems doing so. Can someone out there show me how to program a multiple dimension array that will take from a single dimension array? Listed below is sample code that I am trying to get to work. Code: #include <stdio.h> #define SIZE 5 #define SIZE2 5 #define ROWS 2 void transarr4(int *(*(arry)), int * arry2, int row); //Pointer Notation (Multiple Dimension Array) int main(void) { int arr[SIZE] = {100,200,300,400,500}; int arr2[ROWS][SIZE2] = {{},{}}; int *(*(arr3)) = 0; transarr4(arr3,arr,ROWS); //Pointer Notation (Multiple Dimension Array) return 0; } //==================================================================================================== // Pointer Notation (Multi-Dimension Array) //==================================================================================================== void transarr4(int *(*(arry)), int * arry2, int row) { int num = 0; int r = 0; int c = 0; for(r = 0; r < row; r++) { for(c = 0; c < SIZE2; c++) { //arr[r][c] = arr2[c]; *(*(arry)) = arry2[c]; printf("%d",*(*(arry + r)+c)); printf("\n"); } } }
What do you want
arr2
to look like when done?+-----+-----+-----+-----+-----+
| 100 | 200 | 300 | 400 | 500 |
+-----+-----+-----+-----+-----+
| | | | | |
+-----+-----+-----+-----+-----+or
+-----+-----+-----+-----+-----+
| | | | | |
+-----+-----+-----+-----+-----+
| 100 | 200 | 300 | 400 | 500 |
+-----+-----+-----+-----+-----+"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather