C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 24th, 2003, 11:02 AM
notsoevil notsoevil is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 26 notsoevil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
C: constants, variable array length and C99

Okay, I am more than a bit confused. Assuming I have a constant at the beginning of my code, such as:

Code:
const int MAXROLL  = 400;


Why then:

Code:
static char joinedRolls[MAXROLL*1] = {0};  // compiles fine

static char joinedRolls[MAXROLL] = {0};

/* produces:
main.c:77: variable-sized object may not be initialized
main.c:77: warning: excess elements in array initializer
main.c:77: warning: (near initialization for `joinedRolls')
main.c:77: storage size of `joinedRolls' isn't constant
main.c:77: size of variable `joinedRolls' is too large
*/


What is the difference between MAXROLL*1 and MAXROLL, since mathematically they are equivalent? I know you can't have variable length arrays, but why does the above happen?

In another part of my code I do this:

Code:
  int dieNumberAsInt = atoi(dieNumber);
  int dieSidesAsInt  = atoi(dieSides);
  int rolls[dieNumberAsInt];


And somebody on comp.lang.c commented that I must have a C99 compiler. To me it seems that this looks more like creating an array of variable length since dieNumber and dieSides can be different with each input (this is being looped through). Yet, this compiles just fine.

Along the lines of "you must be doing C99", my loops look like this:

Code:
    int i;
    for (i = 1; i <= dieNumberAsInt; i++) {


It was suggested in comp.lang.c that:

"If you're going to use C99 features such as variable length arrays and
mixed-in declarations, you might as well be consistent and write

for (int i = 1; i <= dieNumberAsInt; i++) {..."

However, gcc complains:

"`for' loop initial declaration used outside C99 mode"

So, since I can't initialize an array with a const int (unless I multiply it by 1 for some reason), and I can create variable length arrays (as possibly illustrated by the atoi section above), AND since I can't write for loops in a sensible manner .. .. er, what the heck!? I don't even know what to ask here. I don't know what is going on. Am I mixing C99 and .. er, whatever else? Is that my problem?

If this helps: gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

Reply With Quote
  #2  
Old June 24th, 2003, 01:25 PM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
I thought gcc had supported variable-length arrays as a non-standard extension for some time, C99 or otherwise, is that not the case?

Maybe check out the gcc documentation.

Reply With Quote
  #3  
Old June 24th, 2003, 01:38 PM
notsoevil notsoevil is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 26 notsoevil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The program compiles just fine with g++ with either change to the initialization of joinedRolls. Not that explains anything, because I still don't understand the things pointed out, but I thought I'd mention it.

Reply With Quote
  #4  
Old June 24th, 2003, 04:05 PM
BigBadBob BigBadBob is offline
status unknown
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 262 BigBadBob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
My point was that gcc has (I believe) accepted variable length arrays for some time (as a non-standard language extension). Not just for C99. So the fact that it accepts variable length arrays tells us nothing with respect to C99.

That it then complains that you're declaring a variable within a for loop (which is only permitted in C99, not C89) suggests that you're compiling in C89 mode, with the permitted variable length array extension that gcc provides. It recognises that C99 permits such a declaration but isn't expecting to compile C99 code.

That seems to me to be consistent with what you described.

Reply With Quote
  #5  
Old June 24th, 2003, 08:46 PM
notsoevil notsoevil is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 26 notsoevil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ah, my mistake entirely then with regards to the for loop.

If gcc is allowing variable length arrays, why the problem with that MAXROLLS section? Why can I declare joinedRolls[MAXROLLS*1] but not joinedRolls[MAXROLLS] .. and the warning says that a variable sized object may not be initialized .. .. but I thought, as you said, that gcc -does- support variable sizes?

I guess I'm just confusing all the different implementations (basic like C89 and C99, but also compiler specific).

Thanks for the input.

Reply With Quote
  #6  
Old June 24th, 2003, 09:22 PM
Lord Avalon Lord Avalon is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 Lord Avalon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Lord Avalon
I think the answer to your question is that compiler does not support variable sized arrays...and a const variable is still a variable..actually a const variable can be changed using pointers....but either way the compiler still sees it as a variable. When you multiply it by one, it is casting it as a constant 'number'. As for the for loop, i think the other user was just trying to point out that you can initialize a variable within a loop with that compiler. Hope this answers your question.

Reply With Quote
  #7  
Old June 25th, 2003, 07:40 AM
notsoevil notsoevil is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 26 notsoevil User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Now if I can just figure out how to get it to initialize a variable within that for loop. I've not set up gcc any way besides default, so if it isn't doing it I certainly don't know why.

I'm new to C (but not programming), so there's no doubt I'll run into more oddities such as this.

Reply With Quote
  #8  
Old June 25th, 2003, 10:42 AM
3dfxMM 3dfxMM is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2002
Posts: 266 3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level)3dfxMM User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 19 h 28 m 33 sec
Reputation Power: 13
You can initialize the variable in the for loop. You just can't define it there.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > C: constants, variable array length and C99


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway