Solved

Declaring arrays

Posted on 1998-10-05
4
329 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
[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 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

630 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