Skip to content

Instantly share code, notes, and snippets.

@jabbalaci
Created May 6, 2023 12:21
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 jabbalaci/7e0bc5fff530dd6ae8e8f9ba39db947d to your computer and use it in GitHub Desktop.
Save jabbalaci/7e0bc5fff530dd6ae8e8f9ba39db947d to your computer and use it in GitHub Desktop.
Simple dynamic array in C
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *tomb = NULL;
int n = 0; // elemszám
while (1)
{
int szam;
printf("Pozitiv szam (kilepes: 0): ");
scanf("%d", &szam);
if (szam == 0)
{
break;
}
tomb = realloc(tomb, (n+1) * sizeof(int));
tomb[n] = szam;
++n;
}
puts("A megadott szamok:");
for (int i = 0; i < n; ++i) {
printf("%d ", tomb[i]);
}
puts("");
free(tomb);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment