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.

4
  • 1
    If you can't use variable length array then info *student = malloc(n * sizeof *student); Commented Jun 28, 2022 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". Commented Jun 28, 2022 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 Commented Jun 28, 2022 at 15:16
  • i am not greedy but could you explain it and a example. Commented Jun 28, 2022 at 15:23

1 Answer 1

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 ) );
    //...
}
Sign up to request clarification or add additional context in comments.

4 Comments

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. Getn 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!
@WilliamPursell It is just a demonstration of the array declaration or of its allocation. Other details are omitted.:)
i have question what is "%zu" and size_t because i know %u, %p.
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.