Guest User

Untitled

a guest
Jan 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void new_matrix(int **matrix, int linhas, int colunas){
  5.  
  6.     // aloca memoria para as linhas
  7.     if ( (matrix = (int**)malloc(linhas * sizeof(int*))) == NULL )  {
  8.         printf("erro de alocação 01\n");
  9.     }
  10.     printf("matrix alocado em %d\n", (int)matrix);
  11.    
  12.     for (int i = 0; i < linhas; i++) {
  13.  
  14.         if ( (matrix[i] = (int*)malloc(colunas * sizeof(int))) == NULL){
  15.             printf("erro de alocação 02 \n");
  16.         ;   break;
  17.         }else{
  18.             for (int j = 0; j < colunas; j++){
  19.                 matrix[i][j] = 0;
  20.             }
  21.         }
  22.     }
  23.    
  24. }
  25.  
  26. int **matrix_global = NULL;
  27.  
  28. main(){
  29.     new_matrix (matrix_global, 10, 10);
  30.  
  31.     printf("matrix alocado em %d\n", (int)matrix_global);
  32.    
  33.     //show_matrix(matrix_global, 10, 10);
  34. }
Add Comment
Please, Sign In to add comment