Solved

What don't I get about variable declaration?

Posted on 2002-07-08
4
176 Views
Last Modified: 2010-04-15
I'm writing a C program that starts like this:

#include <stdio.h>
#define ARRAY_LENGTH 20

int main(void) {
     int al = ARRAY_LENGTH;
     int n[al];
 ...

Well the code won't let me do this.  It doesn't see al as equalling ARRAY_LENGTH or 20.  I thought it was a Borland glitch, but MSVC gives the same error!

I know C has that voodoo that requires the variables to be declared in the program "head" instead of any old place.  It's a 100 point question, so I'm not only looking for how to make the above code work, but a concrete explanation of what the rules are for declaring variables.

Thanks,

Raydot
0
Comment
Question by:Raydot
  • 2
4 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 100 total points
ID: 7140231
Well, I'm not sure if it works for C compilers too, but
with MSVC++ you'd need to make the 'al' const, i.e.:

     int al1 = ARRAY_LENGTH;
     const int al2 = al1;
     const int al3 = ARRAY_LENGTH;
     int n1[al1];     // error
     int n2[al2];     // error
     int n3[al3];     // OK ... a real const

This is because the length of the array must be exactly
known at compile time ... therefor the size must be a const.
An 'int' is per define no const. I think the compiler tries
to replace the variable within the [] with the const number
represanted by the variable which is only possible with
'const int al3 = ARRAY_LENGTH'.

hope that helps,

ZOPPO
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7140303
Raydot: If what you're basically looking for is to create an array whose length is only known at runtime, it isn't possible to do it directly--a declared array in C must be of a fixed length; there are no exceptions to this rule. If you want to dynamically size an array you're going to have to do it manually, e.g.:

int *n;
int al = 100;

n = malloc(sizeof(int) * 100);
n[0] = 13;
n[99] = 127;

or something like that. You can use code like this because a C array is defined as being just a pointer to the first element of the array. Obviously you're going to have to remember to free() the array when you've finished using it!
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 7140305
Correction: the line

n = malloc(sizeof(int) * 100);

should be

n = malloc(sizeof(int) * al);

Sorry about that...
0
 
LVL 3

Author Comment

by:Raydot
ID: 7140858
I'm not sure you gave me 100 points worth of answer, considering the question, but I did actually find this exact answer this morning, so you must be right!

Thanks,

Raydot.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now