Skip to content

Instantly share code, notes, and snippets.

@Madrigal
Last active December 21, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Madrigal/6385786 to your computer and use it in GitHub Desktop.
Save Madrigal/6385786 to your computer and use it in GitHub Desktop.
[WTF] Unexpected behavior in C, prints full matrix, no warnings
/*
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