Znupi

May 9th, 2009, 09:08 AM

Really basic code. It allocates memory for a nxn two-dimensional array and *tries* to fill it out with zeros:

#include <stdio.h>

#include <stdlib.h>

int main() {

short unsigned int n, **a, i, j;

printf("n: "); scanf("%hu", &n);

a = malloc(sizeof(short unsigned int) * n);

for (i=0; i < n; i++) {

a[i] = malloc(sizeof(short unsigned int) * n);

for (j=0; j < n; j++) a[i][j] = 0;

}

for (i=0; i < n; i++) {

for (j=0; j < n; j++) {

printf("%hu ", a[i][j]);

}

puts("");

}

return 0;

}

But it doesn't make it, output:

n: 5

41136 466 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

What is wrong? I can't figure it out for the life of me.

#include <stdio.h>

#include <stdlib.h>

int main() {

short unsigned int n, **a, i, j;

printf("n: "); scanf("%hu", &n);

a = malloc(sizeof(short unsigned int) * n);

for (i=0; i < n; i++) {

a[i] = malloc(sizeof(short unsigned int) * n);

for (j=0; j < n; j++) a[i][j] = 0;

}

for (i=0; i < n; i++) {

for (j=0; j < n; j++) {

printf("%hu ", a[i][j]);

}

puts("");

}

return 0;

}

But it doesn't make it, output:

n: 5

41136 466 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

What is wrong? I can't figure it out for the life of me.