Solved

What don't I get about variable declaration?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

792 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