I'm trying to create a program in C which, after every even number, will add a "0". But I have a problem. If I insert for example only even numbers (5 or more numbers) the program crashes.
Below is the program I have right now.
I would like some indications or a code sample to point out what I did wrong and how I can fix it.
void main()
{
int *a, i, n, m;
printf("dimensiune=");
scanf_s("%d", &n);
a = (int*)malloc(n*sizeof(int));
for (i = 0; i < n; i++)
{
printf("a[%d]=", i + 1);
scanf_s("%d", &a[i]);
}
for (i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
{
n++;
a = (int*)realloc(a, n*sizeof(int));
for (m = n - 1; m > i;m--)
{
a[m + 1] = a[m];
}
a[i + 1] = 0;
i++;
}
}
printf("\n currently you have %d numbers in this string\n", n);
printf("your string \n");
for (i = 0; i < n; i++)
{
printf("a[%d]=%d\n", i + 1, a[i]);
}
}