Solved

What don't I get about variable declaration?

Posted on 2002-07-08
4
186 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 31

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

New My Cloud Pro Series - organize everything!

With space to keep virtually everything, the My Cloud Pro Series offers your team the network storage to edit, save and share production files from anywhere with an internet connection. Compatible with both Mac and PC, you're able to protect your content regardless of OS.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

919 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

21 Experts available now in Live!

Get 1:1 Help Now