Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * DMatrix.c
- *
- * Created on: Mar 14, 2013
- * Author: none
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void print_int_array (int *src, size_t size){
- int i;
- for (i = 0; i < size; i++){
- printf("-%i) [%p] -> (%d) ", i, (src + i), src[i]);
- }
- printf("\n\n");
- }
- int main (int argc, char **argv){
- int **first_matrix = NULL;
- int ****double_matrix = NULL;
- int *array_to_copy;
- int rows, cols;
- int i, j;
- puts("Enter rows and the cols");
- scanf("%d\n%d", &rows, &cols);
- //WHAT IS first_matrix? POINTER TO INT*, SO CAST WILL BE INT**
- first_matrix = (int**)malloc(sizeof(int*) * rows);
- //WHAT IS first_matrix[i]? POINTER TO INT, SO CAST WILL BE INT*
- for (i = 0; i < rows; i++){
- first_matrix[i] = (int*) malloc (sizeof(int) * cols);
- }
- //OK, IN first_matrix EACH ELEMENT IS OF TYPE INT*, SO MANY POINTERS TO INT (OR MANY INT ARRAYS). I'M GOING TO USE array_to_copy
- //WHICH HAS cols TO COPY IT ON EACH first_matrix[i]
- array_to_copy = (int*) malloc (sizeof(int) * cols);
- for (i = 0; i < cols; i++){
- array_to_copy[i] = i;
- }
- //LET'S CROSS EACH first_matrix[i] AND COPY IT'S VALUE FROM array_to_copy
- for (i = 0; i < rows; i++){
- for (j = 0; j < cols; j++){
- first_matrix[i][j] = array_to_copy[j];
- }
- }
- //LET'S PRINT EACH first_matrix[i]
- puts("array_to_copy:");
- print_int_array(array_to_copy, cols);
- puts("\nfirst_matrix:");
- for (i = 0; i < rows; i++){
- print_int_array(first_matrix[i], cols);
- }
- for (i = 0; i < rows; i++){
- free(first_matrix[i]);
- }
- free(first_matrix);
- free(array_to_copy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement