размер массива может быть определен во время выполнения в c?

Ничто не мешает вам напрямую включать классы и работать с ними, как в обычной настройке PHP. Если он работает в PHP, он будет работать в CodeIgniter.

include(APPPATH.'libraries/Amazon/FPS/Interface.php');
17
задан Levent Divilioglu 14 February 2017 в 16:56
поделиться

5 ответов

Array sizes need to be known with ANSI 89 C. The 99 version of the spec removed this limitation and allowed for variable sized arrays.

Here is the documentation no the GNU version of this feature

22
ответ дан 30 November 2019 в 12:36
поделиться

If you need to allocate an array with dynamic size, you have to get it from the heap, with malloc().

int *a = (int*)malloc(sizeof(int) * s)
5
ответ дан 30 November 2019 в 12:36
поделиться

You are confusing two things here.

1) Determining the size of an already allocated array (which your title implies): divide sizeof() for the total by the size of one (say, the first) element:

 sizeof(a)/sizeof(a[0])

2) Dynamically allocating memory as your question asks:

 int *a = (int*)malloc( s * sizeof(int) );
1
ответ дан 30 November 2019 в 12:36
поделиться

This code is supported by C99 language specification. This code is also supported by GCC compiler in C89/90 mode as an extension.

So, the answer to your question (why it "works") depends on how you are compiling it. In general case, this will not even compile by a C89/90 compiler.

1
ответ дан 30 November 2019 в 12:36
поделиться

Its important to understand how memory is allocated to variable by a compiler to give proper answer to your question. There are two modes in which memory is allocated to variable, it can be on a heap or it can be on a stack. Memory on a heap is allocated dynamically. So a variable that is allocated memory on a heap can be given its size during run time.

The arrays in case of C are given memory on a stack. For providing memory on a stack the size of the memory should be known to the compiler during compile time. So that during run time that much memory can be set aside for the variable on the stack. That is the reason you cannot decide the size of the array at run time as far as C language is concerned.

0
ответ дан 30 November 2019 в 12:36
поделиться
Другие вопросы по тегам:

Похожие вопросы: