Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6882325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:14:21+00:00 2026-05-27T05:14:21+00:00

See sometimes I have typedef struct _student { // some data } STUDENT; NOW

  • 0

See sometimes I have

typedef struct _student
{
            // some data
} STUDENT;

NOW

main()
{
   int noOfStudent;
   STUDENT *b;

   //some work

   noOfstudent = this_much_student();
   STUDENT a[noOfStudent];    // way 1
   b=(STUDENT*)malloc(sizeof(STUDENT)*noOfStudent); // way 2

}

Somewhere I read that all variables should be defined at the beginning of a function and the defination of variables in the middle of function should be ignored, So in such condition

does way1 is good ? or way2 is good ? and why?(Justify)

Edit :
i am coding to target c89 compiler and i want the scope is limited to this function only

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:14:22+00:00Added an answer on May 27, 2026 at 5:14 am

    The first way:

    STUDENT a[noOfStudent];
    

    defines a variable-length array. This is a C99-only feature.* The array lives on the stack, and is automatically cleared up once it goes out of scope (e.g. when the function ends). One disadvantage is that if you need a very big array, you will probably cause a stack overflow.

    The second way:

    b=(STUDENT*)malloc(sizeof(STUDENT)*noOfStudent);
    

    should probably be rewritten as:

    b=malloc(sizeof(*b)*noOfStudent);
    

    Either way, it dynamically creates memory on the heap. This avoids the potential for stack overflow, but it does require you to explicitly free() the memory when you’re finished with it.


    * However, many C or C++ compilers will offer it as a non-standard extension.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I see and have used the following variation for a fast divide in
I sometimes see methods in the .net framework prefixed with Try e.g. int.TryParse(..). I
I'm using StringTemplate to generate some xml files from datasets. Sometimes I have more
In subsystem design, I sometimes see software designs that have one high-level class that
I have a strange problem. When I enter my site http://www.dinomuhic.com I sometimes see
When adding a reference to a project I sometimes see the message... The Target
In code, I sometimes see people specify constants in hex format like this: const
When creating iPhone apps in simulator, I sometimes see messages like Springboard failed to
When is it a good idea to use PHP_EOL ? I sometimes see this
I see Oracle procedures sometimes written with AS, and sometimes with IS keyword. CREATE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.