#include <stdio.h>
#include <stdlib.h>

void init_matrix ( double* m, unsigned n )
{
    unsigned i, j;
	for ( i = 0; i < n; i++ )
	{
		for ( j = 0; j < n; j++ )
		{
			m [ i * n + j ] = n;
		}
	}
}

void display_matrix ( double* m, unsigned n )
{
	unsigned i, j;
	for ( i = 0; i < n; i++ )
	{
		for ( j = 0; j < n; j++ )
		{
			printf ("%3.0lf", m [ i * n + j ] );
		}
		putchar('\n');
	}
	putchar('\n');
}

void free_matrix ( double* m )
{
	free ( m );
}

int main(void)
{
	double* M = NULL;
	const int nmax = 10;
	int x, y, n;

	for ( n = 1; n <= nmax; n++ )
	{
		double* temp = realloc ( M, n * n * sizeof(double));
		if ( temp == NULL )
		{
			// realloc liefert NULL -> Fehlerbehandlung
			// Zugriff auf M möglich, um z.B. die Daten zu retten, etc.
		}
		else
		{
			M = temp; // Oki doki
		}

		init_matrix ( M, n );
		display_matrix ( M, n );
	}

	free ( M );
	return 0;
}