0

I want to create a 'n' size of array structure but I don't know how to do it. I always create static like this

typedef struct
{
  int id;
  char name[25];
  char dob[11];
}info;
info student[5];

here i want it info student[n] but it doesn't work because it takes only integer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
jk.
  • 1
  • 4
  • 1
    If you can't use variable length array then `info *student = malloc(n * sizeof *student);` – Weather Vane Jun 28 '22 at 15:15
  • 1
    Welcome to SO. "It takes only integer" does not make sense. It only takes a *constant expression* which means "a compile time constant expression". – Gerhardh Jun 28 '22 at 15:15
  • 1
    Variable length arrays can only be used withing functions, not on file scope. Normally it is better to use dynamic memory allocation as suggested by Weather Vane – Gerhardh Jun 28 '22 at 15:16
  • i am not greedy but could you explain it and a example. – jk. Jun 28 '22 at 15:23

1 Answers1

1

It seems you mean the following code

typedef struct
{
  int id;
  char name[25];
  char dob[11];
}info;
info student[n];

where n is some variable of an integer type.

This declaration of an array declares a variable length array. Variable length arrays may have only automatic storage duration . So you may declare such an array in a function as for example

typedef struct
{
  int id;
  char name[25];
  char dob[11];
}info;

int main( void )
{
    size_t n;

    printf( "Enter the array size: " );
    scanf( "%zu", &n );

    info student[n];
    //...
}

You may not declare a variable length array in a file scope.

Pay attention to that the value of the variable n shall be a positive number. Another peculiarity is that you may not initialize elements of a variable length array in its declaration.

If your compiler does not support variable length arrays then you need to allocate an array of structures dynamically like for example

typedef struct
{
  int id;
  char name[25];
  char dob[11];
}info;

int main( void )
{
    size_t n;

    printf( "Enter the array size: " );
    scanf( "%zu", &n );

    info *student = malloc( n * sizeof( *student ) );
    //...
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I'm so close to upvoting, but cannot because of the demonstration of `scanf` to get a run-time parameter that ought to be from `argv`. Get`n` is some reasonable way that doesn't demonstrate bad practice to the beginner, and I will certainly reconsider. Failing to check the return value of `scanf` almost gets a downvote! – William Pursell Jun 28 '22 at 15:30
  • @WilliamPursell It is just a demonstration of the array declaration or of its allocation. Other details are omitted.:) – Vlad from Moscow Jun 28 '22 at 15:35
  • i have question what is "%zu" and size_t because i know %u, %p. – jk. Jun 28 '22 at 15:36
  • @jk. For example the operator sizeof yields a value of the unsigned integer type size_t. For example if you will write printf( "sizeof( int ) = %zu\n", sizeof( int ) ); then teh expression sizeof( int ) has the type size_t. To output values of this type you have to use the conversion specifier zu. – Vlad from Moscow Jun 28 '22 at 15:39