Solved

Declaring arrays

Posted on 1998-10-05
4
326 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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 how to clear a vector as well as how to detect empty vectors in C++.

809 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