Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > dynamic declaration of struct array

Thread Tools

dynamic declaration of struct array

Allin Cottrell
Posts: n/a
Noah Roberts wrote:
> Tim wrote:
> Stationers stats[num];
>> 12 }
>> The error message says "constant expression required" and highlights
>> line 11. I want to declare it dynamically during runtime, could
>> someone explain how to do this please.

> Stationers *stats = (Stationers*)malloc(num * sizeof(Stationers));

Or, better (losing the spurious cast):

Stationers *stats = malloc(num * sizeof *stats);

Allin Cottrell
Department of Economics
Wake Forest University, NC

Reply With Quote
Posts: n/a
Cheers for the help, I'm now using the malloc function and its working well.
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
struct declaration into another struct slocum C++ 3 04-11-2008 09:42 AM
Initialize pointer-to-struct declaration to an unnamed struct? Ehud Shapira C++ 20 06-30-2007 04:10 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM