Solved

What don't I get about variable declaration?

Posted on 2002-07-08
4
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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 and use structures in the C programming language.
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.
Suggested Courses

636 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