Is this an example of static memory allocation or dynamic memory allocation?

I investigated a lot of static and dynamic memory allocation, but there is still confusion in that:

int n, i, j;
printf("Please enter the number of elements you want to enter:\t");
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++)
{
    printf("a[%d] : ", i + 1);
    scanf("%d", &a[i]);
}

Is it suitable int a[n]for allocating static or dynamic memory?

+4
source share
3 answers

No auto access

+1
source

The C standard does not talk about dynamic allocation (or static allocation, for that matter). But it determines the duration of storage: static, automatic, stream and distributed. Which dictates how long the object lives (part of the data) (can be used).

  • , . ( ) static .

  • - , , ( , , for ).

  • , malloc . () malloc free. , , .

a . , , . , .

+12

int a[n] - ,

.

#include <stdio.h>
#include <string.h>

int main(void) 
{
    const size_t N = 10;

    for ( size_t i = 1; i <= N; i++ )
    {
        char s[i];

        memset( s, '*', sizeof( s ) );

        printf( "%*.*s\n", ( int )i, ( int )i, s );
    }

    return 0;
}

*
**
***
****
*****
******
*******
********
*********
**********

, , s[n] .

+2
source

Source: https://habr.com/ru/post/1690641/


All Articles