Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Daishim

C/C++ Arrays

This topic is 5937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In VB you could declare an array like so
   Dim intArray(1 to 4) As Integer   
Can this be done in C/C++ so that keeping track of things (especially objects) would be easier by starting with 1?

Share this post


Link to post
Share on other sites
Advertisement
No ... although if you really want your arrays to start at one, I suppose you could just make them one bigger, and not use the 0th item. I don''t think that''s a good idea, though.

~~~~~~~~~~
Martee

Share this post


Link to post
Share on other sites
  
#include <stdio.h>

int main(int argc, char *argv[])
{
int* intarray = new int[4];
--intarray;

for (int x=1; x<5; ++x)
{
intarray[x] = x*x;
}

printf("Array[1] %d\n", intarray[1]);
printf("Array[2] %d\n", intarray[2]);
printf("Array[3] %d\n", intarray[3]);
printf("Array[4] %d\n", intarray[4]);

return 0;
}

Not at all recommended, by the way Bad programming practice, really. This example was just provided to further understanding of the language

Share this post


Link to post
Share on other sites
You could always create a template array class and overload the [] operator to whatever you wanted it to. You could also include bounds checking for safety....


  
// Template array class. Note... this was written off the top of

// my head, may require debuggering.


template <class T>
class CArray
{
public:
CArray(int size)
{
data = new T[size];
maxItems = size-1;
}
~CArray()
{
delete[] data;
}
T& operator[] (int index)
{
//index--; // uncomment this line to make the array 1 based

if(index > maxItems)
return data[maxItems];
elseif (index < 0)
return data[0];
else
return data[index];
}
private:
T* data;
int maxItems;
};



Then you can instantiate an array of any type like this...

  
CArray<int> myIntegerArray;
CArray<char> myCharArray;
CArray<float> myFloatArray;


etc...

Share this post


Link to post
Share on other sites
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?

Share this post


Link to post
Share on other sites
1. You cannot resize a normal array at all. Ever.
2. You cant resize a dynamically allocated array* but you can make a new array and copy the contents across, then destroy the old one.
3. If you use something like the template class I wrote above, you could include methods that take care of this array copying resizing stuff internally.
4. Ideally, your arrays should be big enough to fit what you need to put in them. If your array is likely to change size a lot, and is likely to get very large, you may be better off using a linked list.

*ignoring functions like realloc() which are dangerous, and they just do the samething anyway under the hood.

Share this post


Link to post
Share on other sites
Sandman:

How would i go about creating the same tamplate for a 2d array?

i need a place to store outside inputs and as you anderstand there can be any number of inputs so i can not limit the array size.
I''m just not sure how to overload the second []!

Thanks

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?


use a std::vector

[Questions (STFW) | GDNet Start Here | GDNet Search | Forum FAQ | Google | Asking Smart Questions ]
[Docs (RTFM) | MSDN | SGI''s STL | OpenGL | File formats]
[C++ Must Haves (RTFS) | MinGW | Boost | Loki | FLTK | SDL ]

Stolen from Magmai Kai Holmlor, who held it from Oluseyi, who was inspired by Kylotan...

Share this post


Link to post
Share on other sites
quote:
Original post by Daishi
Is it possible to redimension an array like VB''s ReDim statement? Or must I manually store the data temporarily and delete and remake the array to what is needed?
you can use realloc(), or better yet, std::vector

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sadly saying it once is not enough: use std::vector

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!