While trying to learn C by myself, I came across this simple program that I want to develop. It just tries to make use of pointer to pointer arrays to make something that resembles matrices. I'm compiling on Windows and when I run it, it just crashes, meanwhile, trying this code on Linux it says segmentation fault, is this because of the function arguments that are arrays? What am I doing wrong here?
#include <stdio.h>
#include <stdlib.h>
void initializeArray(float** array, int size);
void printArray(float** array, int size);
int main()
{
float** array_1 = NULL;
int array_size = 3;
initializeArray(array_1, array_size);
// Free memory from array
for (int i = 0; i < array_size; i++)
{
free(array_1[i]);
}
free(array_1);
return 0;
}
void initializeArray(float** array, int size)
{
array = malloc(size * sizeof(float*));
if (array)
{
for (int i = 0; i < size; i++)
{
array[i] = malloc(size * sizeof(float));
if (!array[i])
{
exit(0);
}
}
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array[i][j] = 0;
}
}
}
void printArray(float** array, int size)
{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
printf("%f\t", array[i][j]);
}
printf("\n");
}
}