Created
May 6, 2023 12:21
-
-
Save jabbalaci/7e0bc5fff530dd6ae8e8f9ba39db947d to your computer and use it in GitHub Desktop.
Simple dynamic array in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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