Last active
December 21, 2015 23:59
-
-
Save Madrigal/6385786 to your computer and use it in GitHub Desktop.
[WTF] Unexpected behavior in C, prints full matrix, no warnings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
WTF behavior in C. At least not what I expected. | |
Prints full matrix with just one call to the index | |
*/ | |
#include "stdio.h" | |
#include "stdlib.h" | |
void print_square_matrix(int* matrix, int size); | |
int main(int argc, char const *argv[]) | |
{ | |
printf("Matrix \n"); | |
int SIZE = 50; | |
// Allocate space for the matrix | |
int** matrix; | |
matrix = (int **) malloc (sizeof(int*) * SIZE); | |
int i; | |
for (i = 0; i < SIZE; ++i) | |
{ | |
matrix[i] = (int*) calloc (0, sizeof(int) * SIZE); | |
} | |
printf("printing matrix \n"); | |
print_square_matrix(&matrix[0][0], SIZE); | |
return 0; | |
} | |
void print_square_matrix(int* matrix, int size){ | |
int i; | |
int j; | |
for (i = 0; i < size; ++i) | |
{ | |
for (j = 0; j < size; ++j) | |
{ | |
printf("%d", matrix[i]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment