Solved

Declaring arrays

Posted on 1998-10-05
4
324 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

13 Experts available now in Live!

Get 1:1 Help Now