Solved

Declaring arrays

Posted on 1998-10-05
4
327 Views
Last Modified: 2010-04-01
I need to declare an array in a program with the size based on a variable.  However I keep getting the error message "expected constant expression" when I compile the program.  Is it absolutely necessary to declare at compile time how big the array is, or can the size be based somehow on a variable calculated earlier.

Thanks in advance.
0
Comment
Question by:lewin
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
plaroche earned 200 total points
ID: 1174418
Since you're asking this question in the C++ section I'll answer with the new operator.

Let's say you want an array of size doubles;

// Allocation
int      size = 200;
double*  pArray = new double[size];

// Deallocation
delete[] pArray;

As simple as that.
0
 

Author Comment

by:lewin
ID: 1174419
It works but just for my understanding could you explain why you have to use a pointer and why it doesn't work when you use a normal variable.  I'm not all that clear about pointers.  Can I reference the variable as normal later in the program?

Thanks very much.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1174420
1. >> Can I reference the variable as normal later in the program?
Yes in this case, example

pArray[3] = 0.0 ;

2. You can't use a non-pointer to allocate the array dynamic, as C/C++ doesn't support this idea.

double Array[size] ; // invalid

Incidentally, some other languages do

3. Check MFC's CArray or STL's vector, for some array types implemented by the compiler vendor (these are part of "libraries" of pre-written routines that you can take advantage of)
0
 
LVL 3

Expert Comment

by:plaroche
ID: 1174421
You need to use a pointer because what the new operator returns is a pointer.  You can still reference the elements like Answers2000 specified.

Simply the new operator allocates a block of memory dynamically, that is, when the code is executed it encounters an instruction where it is said to allocate 200 doubles. That is done and your variable now points to the first double in that memory block.

Dynamic allocation is explained in a lot of C beginner books, you might want read on that subject. Without dynamic allocation a lot of things cannot be done.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

821 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