A
A
andrey_levushkin2018-10-17 15:13:59
C++ / C#
andrey_levushkin, 2018-10-17 15:13:59

How to create and use a dynamic array?

How to create and use a dynamic array in C?
It’s just that I don’t know in advance how many values ​​will be in it, is it for this that dynamic ones are used?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-10-17
@andrey_levushkin

Let N be the number of elements in the array:

#include "stdlib.h"

...

int * arr = (int*)malloc(sizeof(int) * N);
...
for(int i = 0; i < N; ++i)
   printf("%d ", arr[i]);
printf("\n");
free(arr);

Here, for brevity, checking for memory allocation errors is omitted /

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question