You are on page 1of 11

Matrices Dinmicas en C

Algorimos y Estructuras de Datos II


Leonardo Rodrguez

Arreglo esttico:
int array[100];
Arreglo dinmico:
int *array = calloc(100, sizeof(int));

array

int *array = calloc(4, sizeof(int));


for (int i = 0; i < 4; i++) {
array[i] = 0;
}

array

'a'

char *array = calloc(4, sizeof(char));


for (int i = 0; i < 4; i++) {
array[i] = 'a';
}

'a'

'a'

'a'

array

int* *array = calloc(4, sizeof(int *));


for (int i = 0; i < 4; i++) {
array[i] = NULL;
}

0
array

int* *array = calloc(4, sizeof(int *));


for (int i = 0; i < 4; i++) {
array[i] = NULL;
}
array[2] = calloc(4, sizeof(int));
for (int i = 0; i < 4; i++) {
array[2][i] = 0;
}

int **m = calloc(4, sizeof(int *));


for (int i = 0; i < 4; i++) {
m[i] = calloc(3, sizeof(int));
}
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
m[i][j] = 0;
}
}

Matriz 4x3
4 filas
3 columnas

free(m);

Memory leak!
Primero liberar las filas.

for (int i = 0; i < 4; i++) {


free(m[i]);
m[i] = NULL;
}
free(m);
m = NULL;

Matriz esttica:
int m[100][200];
Matriz dinmica:
int* *m = calloc(100, sizeof(int*));
for (int i = 0; i < 100; i++) {
m[i] = calloc(200, sizeof(int));
}

You might also like