Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic array declaration

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
i am trying to declare array dynamixcally :
DWORD dwThreadId[IThreads];
IThreads is of int type.
But it is giving the error as follows : C:\Windows\Desktop\Copy (2) of Storedprocedure\StoredProcedure.cpp(31) : error C2057: expected constant expression
C:\Windows\Desktop\Copy (2) of Storedprocedure\StoredProcedure.cpp(31) : error C2466: cannot allocate an array of constant size 0
C:\Windows\Desktop\Copy (2) of Storedprocedure\StoredProcedure.cpp(31) : error C2133: 'dwThreadId' : unknown size
waiting for the reply
 
The best and easiest way to declare a dynamic array is to use the VC++ CArray class.

// Add the following #include statement
// to all class header files that use CArray
#include <afxtempl.h>

// In your code, wherever you want to use a
// CArray, declare as follows:
CArray<int,int> myArrayOfInts;

// You can change the 'int' data type for any type
// you want including CString,float,etc. and even
// your own data structures

// It's usually a good idea to set the size of the
// array before you start sticking things into it
myArrayOfInts.SetSize(0,0);

// You can add items to the array easily:
myArrayOfInts.Add(12345);

// When you add items to the array, the array
// will automatically allocate an extra space
// for the new item

// You can find the number of items contained
// in your array:
int arraySize = myArrayOfInts.GetSize();

// Use a typecast to retrieve the information
// from an array element
int someMember = (int)myArrayOfInts.GetAt(0);

// You can remove an element from the array
// by using it's zero-based index
myArrayOfInts.RemoveAt(0);

// Alternatively, you can remove all elements
// in the array:
myArrayOfInts.RemoveAll();

Hope this helps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top