![]() |
![]() |
![]() |
|||||
![]() |
![]() |
![]() |
![]() |
![]() |
|||
| Welcome
to Tech Support Forum home to more then 136,000 problems solved. Issues
have included: Spyware, Malware, Virus Issues, Windows, Microsoft,
Linux, Networking, Security, Hardware, and Gaming Getting your
problem solved is as easy as: 1. Registering for a free account 2. Asking your question 3. Receiving an answer Registered members: * See fewer ads. * And much more..
|
| Want to know how to post a question? click here | Having problems with spyware and pop-ups? First Steps |
|
|||||||
| Programming A discussion forum for programs and programming used in tech-related businesses. |
![]() |
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) |
|
Registered User
|
Accessing values in arrays pointed to by a pointer array. (C++)
So, I guess I feel like kind of a jerk coming here for this. But looking around the rest of the forums, I think this could be a nice new haunt. ^_^
Anywho...I'm having a bit of trouble figuring out how to properly manipulate an array of pointer values that each point to another array. My knowledge of C++ is mediocre at best, and after much searching, I can't seem to lock onto a solid answer for this. My books aren't being very helpful either. This is a tiny but critical portion of a larger project. I'm not asking for a full-blown code or anything, just some assistance in how to manage and manipulate these arrays properly. Let's say I make a pointer array: int* mooPoint[4]. Next, I create an integer array pointed to by mooPoint[0], using the new command: mooPoint[0] = new int[10]. Each pointer in mooPoint[] will point to a different array. Here's where I get caught up. I'd like to access the third value (new int[2]) in the array pointed to mooPoint[2]. Maybe I want to write a value, or print a value, or copy it into something else. I just need to access that value, or any given value for that matter in any of the arrays. Example: /******************************************/ int* mooPoint[4]; for(int i = 0; i < 4; i++) { mooPoint[i] = new int[10]; } //copy int value "x" into position [2] in the array pointed to by mooPoint[2] ????? = x; /******************************************/ I can't for the life of me figure out the proper syntax to do such a thing. I'm really stumped on this one, as I've never had to do anything quite like it before. This is a critical launchpad for the rest of the project. Once I'm able to manipulate the arrays pointed to by a pointer array, it's all gravy. I am confident that once I know how to do this, I can complete the rest of the project as well. Knowing how to do such a simple thing as above, I should be able to devise how to load up the arrays, print them out, and otherwise maniplate them. For reference, this is C++, I'm using MS Visual Studio 6.0, and running Windows XP. Assistance is much appreciated, and thank you for even reading this far. ^_^ Last edited by TheUnlimited; 12-11-2005 at 07:05 PM. |
|
|
|
| Important Information |
|
Join the #1 Tech Support Forum Today - It's Totally Free!
TechSupportForum.com is a leading support website for your computer needs. We offer free, friendly and personalized computer support. Why pay to have your computer fixed when you can do it for free. Join TechSupportforum.com Today - Click Here |
|
|
#2 (permalink) |
|
TSF Enthusiast
Join Date: Dec 2004
Posts: 604
OS: windows xp
|
PHP Code:
Usually it not decleared the way you did it even thoe your way works fine. Usually the code would look something like this. PHP Code:
__________________
|
|
|
|
|
|
#3 (permalink) |
|
Registered User
|
Thank you very much, MGold. ^_^
It figures that it was something as simple as that, while I was sitting here thinking I'd need the dereference asterisk or the reference ampersand. :P I should be able to lift off from here, and knowing that it's really so simple is even better. The particular reason I'm doing it that way is because each array will be of a variable size -- so one array may be 10 slots, and the next one may be 45. Once again, you have my thanks. EDIT: I guess I was rather misleading in my intentions, so here's a clarification on my initial little code blurb. The reason I don't simply make four arrays of different sizes is because the number of arrays is also random, although that is not shown in this code. PHP Code:
Last edited by TheUnlimited; 12-11-2005 at 09:03 PM. |
|
|
|
|
|
#4 (permalink) |
|
Registered User
Join Date: Dec 2005
Posts: 5
OS: XP
|
The code posted by mygodlb2 should work fine. Just some notes:
When you declar an array by the following: Code:
int* mooPoint[4]; 0,1,2,3,4 = 5 elements. The following code implements mygodlb2's advice, which complies and runs fine for me: Code:
#include <iostream>
using namespace std;
int main ()
{
int* mooPoint[4]; //5 element array
int n = 0;
for(int i = 0; i < 4; i++)
{
n = rand(); //generate random number
mooPoint[i] = new int[n]; //make an array of size n associated
//with mooPoint[i]
//Initialize this array with a default value (0)
for (int j = 0; j < n; j++)
{
mooPoint[i][j] = 0;
}
}
int x = 930;
//copy int value "x" into position [2] in the
//array pointed to by mooPoint[2]
mooPoint[2][2] = x;
cout << mooPoint[2][2] << endl;
return 0;
}
Now someone correct me if I'm wrong here, but I think another thing to note would be: Because you use rand() to generate the number of elements in the integer arrays, there is no guarantee mooPoint[2][2] actually exists. Since rand() returns an integer value between 0 and RAND_MAX (32767) -- the number of elements given to the array contained in mooPoint[2] may well only be 1. Last edited by Cache; 12-12-2005 at 09:21 AM. |
|
|
|
|
|
#5 (permalink) | |
|
TSF Enthusiast
Join Date: Dec 2004
Posts: 604
OS: windows xp
|
Quote:
0,1,2,3 = 4 elements. YOU CAN NOT USE 4. it will compile and it even might give you the right answer sometimes but in most cases it should thow you a error.
__________________
|
|
|
|
|
|
|
#6 (permalink) | |
|
Registered User
Join Date: Dec 2005
Posts: 5
OS: XP
|
Quote:
EDIT: Seems I can't edit my other post to correct it either. Last edited by Cache; 12-12-2005 at 10:10 AM. |
|
|
|
|
|
|
#7 (permalink) |
|
Registered User
|
I thank you both from the bottom of my heart. ^_^
Thanks for pointing out that mooPoint[2][2] may not even exist. There is a default minimum/maximum that I will set the RAND() to, so I'll have certain bounds. A separate array will be used to track the number of slots in each of the arrays during generation time, before the random number is overwritten. You have my greatest gratitude, and hopefully this will allow me to complete the rest of the project. It's a very small part of it, and I won't bother you all with the rest of the details, but it's also a veritable cornerstone. I probably won't be able to post again until after Wednesay, when it's due. Until then, thanks! ^_^/) |
|
|
|
|
|
#8 (permalink) |
|
Registered User
|
I've got the project to do just about everything I need it to do, thanks to you guys. That really was a cornerstone for the whole thing. 8)
Another question, on related topic, though. I need to make some random-sized pointer arrays now, but I'm constantly getting the errors: error C2057: expected constant expression error C2466: cannot allocate an array of constant size 0 From what I gather, you're not allowed to make arrays of a random size "on the fly." Unfortunately, that's exactly what I need to do, generate random-size arrays each time the program is run. I'm able to do this when using the "mooPoint[i] = new int[n]" method, but not if I do something more like: PHP Code:
PHP Code:
Any methods that I can use to "fool" the compiler? If all else fails, I can try to make a dynamic array function that could generate a random-sized bounded array... |
|
|
|
|
|
#9 (permalink) |
|
Registered User
Join Date: Dec 2005
Posts: 5
OS: XP SP2
|
Hm, random sized array?The compiler needs the size of an array...because they're getting their memory reserved even without initialization (afair)
I recommend using dynamically allocating your array (even though I'd go with blocky allocation,like always allocate medium sized chunks) here's the code: PHP Code:
Hope it helps~ (another edit: sorry I'm sleepy I guess my post is redundant , you already knew that ,right?I'm at my limit of being awake, I'll take a look at your prob later... but I think you can access pointers in your array using an ampersand , &moo_point[2] = 1; // sets the value in [&2] to 1...not sure...too sleepy) Last edited by Sesshou; 12-16-2005 at 10:00 PM. |
|
|
|
|
|
#10 (permalink) |
|
Registered User
|
Thank you, MGold, Cache, and Sesshou. Those tiny little bits of proper programming knowledge that you guys imparted upon me allowed me to complete the rest of the project. They were a tiny but massively important aspect, and is all worked out in the end. ^_^
|
|
|
|
|
|
#11 (permalink) |
|
Registered User
Join Date: Dec 2005
Posts: 5
OS: XP SP2
|
I would have edited my post , but it seems that's only possible for a short time after you've posted...
This is what I've wanted to add: - int * a; /pointer to an int,can be turned into an array by allocating memory you can access those with either *a or a[n] - int ** a; pointer to a pointer,both can be turned into arrays by allocating memory, access : *a[n],**a,a[n][n] etc. think of [n] as one step forward in pointers... using pointers to pointer you can create all sorts of funky arrays like int ** a; a[0][allocate 5 int pointers] (a[0][0] = new int[5]) access a[0][0-4]; a[1][allocate 10 int pointers] (a[1][0] = new int[10]) access a[1][0-9]; ... a[5][allocate 50 int pointers] (a[5][0] = new int[50]) access a[5][0-49]; etc... you have to free the branches first,freeing the root array first would leak memory... to free a 2 dimensional array like a[variable][variable] free a[0-(variable -1)] then free a, I didn't write the detailed code,since you've already finished your project, but I hope this information is still somewhat valuable to you
|
|
|
|
![]() |
| Thread Tools | |
|
|