Solved

Declaring arrays

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now