#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "windows.h"


struct MY_CAR
{
	int year;
	char nazwa[64];
};

size_t pozycja = 0;
MY_CAR* pStruct = NULL;

int fun_create_array(int dim)
{
	if (!pStruct)
	{
		pStruct = (MY_CAR *)malloc(dim * sizeof(MY_CAR));

		if (!pStruct)
			return 0;

		memset((void *)pStruct, 0, dim * sizeof(MY_CAR));
		pozycja = 0;
		return 1;
	}
	return 0;
}



void fun_print_tab(char *title)
{
	size_t ob_poz;

	printf("%s\n\n", title);

	for (ob_poz = 0; ob_poz < pozycja; ++ob_poz)
	{
		printf("\nElement %zu: \n", ob_poz + 1);
		printf("Nazwa: %s\n", pStruct[ob_poz].nazwa);
		
		printf("Rok: %d\n\n", pStruct[ob_poz].year);
	}
}



void fun_free()
{
	if (pStruct)
	{
		free(pStruct);
		pStruct = NULL;
		pozycja = 0;
		printf("\nPamiec zwolniona \n");
	}

}



void fun_fill_array(int dim)
{
	for (int i = 0; i < dim; ++i)
	{
		if (pozycja < dim) {
			printf("Element %d: \n", i);
			printf("Podaj nazwe (max 63 znaki): ");
			scanf_s("%s", pStruct[pozycja].nazwa, (unsigned)_countof(pStruct[pozycja].nazwa));

			printf("Podaj rok: ");
			if (scanf_s("%d", &pStruct[pozycja].year) != 1)
			{
				printf("blad wczytywania roku");
				break;
			}
			pozycja++;
		}
	}


}



int main()
{
	int dim;
	printf("podaj ilosc elementow");
	if (scanf_s("%d", &dim) != 1 || dim <= 0)
	{
		return 1;
	}

	if (fun_create_array(dim))
	{
		fun_fill_array(dim);
		fun_print_tab("wynikowe dane");
		fun_free();
	}
	else {
		printf("blad alokacji pamieci lub tablica juz istniala");
	}
	system("pause");

	return 0;

}