I need to create an array of pointers that point to int arrays. Can someone clarify the proper syntax for me?
27 replies to this topic
#1
Posted 04 January 2011 - 08:25 PM
|
|
|
#2
Posted 05 January 2011 - 02:54 AM
Hi,
I hope this helps!
Munir
int array[100]; int **pointerToArray = & array;
I hope this helps!
Munir
#3
Posted 05 January 2011 - 08:49 AM
I think that this might be what your after.
int i[1000][1000]; // Creates a 2-dimensional arrayBut it sounds as if you don't want to have the array be contiguous in memory. Meaning if the array's dimensions were 1000x1000, you don't want a single chunk of 1,000,000 objects in your RAM cause it might not fit too great. If this is the case, take a look at the following code (if it isn't, ignore the following code).
int * array[1000]; // 1000 is the width of the array
for (int i = 0; i < 1000; ++i)
array[i] = new int[1000]; // 1000 is also the height of the array
// Executed at some point..
for (int i = 0; i < 1000; ++i)
delete [] array[i];
#4
Posted 05 January 2011 - 07:55 PM
Actually, what I'm trying to do is create a 3D array on the heap. and in order to do so, I have to mess around with pointers. Unfortunately, this got pretty confusing.
#5
Posted 05 January 2011 - 08:50 PM
Then perhaps this is closer to what your after.
int a[1000][1000][1000]; // 3-Dimensional arrayAnd the non-contiguous version..
int *** a = new int ** [1000];
for (int i = 0; i < 1000; ++i) a[i] = new int*[1000];
for (int i = 0; i < 1000; ++i)
for (int j = 0; j < 1000; ++j)
a[i][j] = new int[1000];(You should of course be using vectors, but you did explicitly say array so I assumed you were talking about true arrays)
#6
Posted 05 January 2011 - 08:51 PM
Why are vectors any better to use?
#7
Posted 05 January 2011 - 09:20 PM
Vectors provide automation of various common tasks done with arrays with negligible overhead. A vector will also automatically deallocate itself which is immensely useful.
I will go ahead and say, always use vectors and never use arrays. There will be exceptions to this of course, but if you are a beginning programmer you don't have to worry about such exceptions.
Reference on Vectors
I will go ahead and say, always use vectors and never use arrays. There will be exceptions to this of course, but if you are a beginning programmer you don't have to worry about such exceptions.
Reference on Vectors
#8
Posted 05 January 2011 - 09:33 PM
I see... thank you for showing me this. However, it is possible that I may end up working on something with very limited space. Also, I'd like to learn to do it the hard way before I do it the easy way. I'll play around with these arrays some more, and if I need help I'll post back.
#9
Posted 05 January 2011 - 09:42 PM
Alright, however, using arrays over vectors does not save space. A vector will store the length of the array, which means it is storing sizeof(size_t) extra bytes of space, however, you'd need this information almost certainly anyways. Also, the size of an int which is usually 4 bytes, is extremely tiny compared to your array. Learning is fine, but if your not exploring how to work with arrays, use vectors, it is always the better choice. (Again, exceptions: yes, worry: no).
#10
Posted 07 January 2011 - 04:10 PM
FYI if you're working on something with "very limited space" you're probably going to want to use straight C. Since vectors are a C++ thing, you might have to use regular arrays anyway.
sudo rm -rf /
#11
Posted 07 January 2011 - 09:13 PM
byte*** bob = new byte**[4];
bob[0] = new byte*[5];
bob[0][0] = new byte[5];
I get an error over the first bracket here: "expected an expression". Any idea what I'm doing wrong?
bob[0][0] = {0, 0, 0, 0, 0};
Untitled.png 3.36K
9 downloads
#12
Posted 07 January 2011 - 09:16 PM
byte*** bob = new byte**[4];
bob[0] = new byte*[5];
bob[0][0] = new byte[5]; [COLOR="blue"]/* why are you doing this? */[/COLOR]
[COLOR="blue"]/* Illegal unless you're doing it all at once at initialization */[/COLOR]
bob[0][0] = {0, 0, 0, 0, 0};
sudo rm -rf /
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









