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 7619911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:48:32+00:00 2026-05-31T03:48:32+00:00

I’m trying to create an array of structs, of which the array size is

  • 0

I’m trying to create an array of structs, of which the array size is defined by the user in the program. eg. p[0], p[1], p[2]…..

typedef struct
{
int score;
}player;

void main()
{
    int numPlayers;

    printf ("\nEnter number of players (1 - 4)\n");
    scanf ("%d", &numPlayers);

}

I’ve tried doing it with both

player p[numPlayers];

and

player *p=malloc(numPlayers*sizeof(player));

but both won’t compile.
Can anyone see what’s going wrong here?

Edit: I’m using VS2010.
I’m getting “expression must have a constant value” for the first one, and “a value of type “void*” cannot be used to initialise an entity of type “player*” for the second one.

  • 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-31T03:48:34+00:00Added an answer on May 31, 2026 at 3:48 am

    The player p[numPlayers]; approach calls for a “variable length array”. This is a feature which appeared in the GNU C dialect many years ago, and was adopted into the C language in 1999. To get that to compile you need a compiler which recognizes this as an extension, or a C99 compiler. Variable Length Arrays have a downside: they are usually implemented by allocating the memory on the stack. There is no way to detect whether or not there is enough memory.

    The malloc calling syntax you have is fine:

    player *p=malloc(numPlayers*sizeof(player));
    

    However, if you want to write this anywhere in your function, you need to be using a C99 compiler, or a compiler that accepts inter-mingled statements and declarations (like GNU C which has had that as an extension for years before C99 and accepts it by default. In C90, you have to declare the pointer in the block of declarations at the top of the function (or braced statement): player *p = NULL;. Then later, after the number of players is known, assign to it p = malloc ....

    You should post the actual program that doesn’t compile. Without that we are only guessing!

    Moreover, you have some problems.

    Firstly, if you want to call malloc, you should include the header <stdlib.h> where malloc is declared.

    Secondly, main should return type int not void.

    Also, check the return value of scanf. The scanf function is bad for interactive input. For instance if the user hits Enter, scanf keeps waiting for input, looking for those numeric characters, which is unfriendly. scanf also has no error checking in %d. If you enter a number that doesn’t fit into the type int the behavior is simply undefined. This type of input handling is only suitable for quick-and-dirty “throwaway” programs written for the author’s own use.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.