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:
* Get free support
* Communicate privately with other members (PM).
* Removal of this message
* 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
Go Back   Tech Support Forum > The IT Pro > Programming
User Name
Password
Site Map Register Donate Rules Blogs Mark Forums Read


Programming A discussion forum for programs and programming used in tech-related businesses.

Reply
 
LinkBack Thread Tools
Old 12-11-2005, 06:46 PM   #1 (permalink)
Registered User
 
Join Date: Dec 2005
Location: New England
Posts: 139
OS: Windows XP Home SP3

My System

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.
TheUnlimited is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
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

Old 12-11-2005, 07:48 PM   #2 (permalink)
TSF Enthusiast
 
Join Date: Dec 2004
Posts: 604
OS: windows xp


PHP Code:
mooPoint[2][2]=7;
cout<<mooPoint[2][2]<<endl
would put 7 in the spot you wanted in your 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:
#include <iostream> //for input  and output

using namespace std;

int main ()
{
    
int mooPoint[10][10];

mooPoint[2][2]=7;
cout<<mooPoint[2][2]<<endl;

return 
0;


__________________
mgoldb2 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-11-2005, 08:37 PM   #3 (permalink)
Registered User
 
Join Date: Dec 2005
Location: New England
Posts: 139
OS: Windows XP Home SP3

My System

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:
#include <iostream>

using namespace std;

int main ()
{
    
intmooPoint[4];  //create the array of pointers
    
int n 0;
    for(
int i 04i++)
    {
        
rand();        //generate random number
        
mooPoint[i] = new int[n];  //make an array of size n associated
                                   //with mooPoint[i]
    
}

    
int x 930;

    
//copy int value "x" into position [2] in the 
    //array pointed to by mooPoint[2]
    
????? = x;

    return 
0;

Forgive me if my code isn't pristine. It's been ages since I've been given a coding assignment.

Last edited by TheUnlimited; 12-11-2005 at 09:03 PM.
TheUnlimited is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-12-2005, 09:19 AM   #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];
You are making an array with 5 elements, not 4. Because array indexing starts at 0.

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;
}
You can see I aslo give each element of the integer arrays a default value. This would make it easier to debug.

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.
Cache is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-12-2005, 09:51 AM   #5 (permalink)
TSF Enthusiast
 
Join Date: Dec 2004
Posts: 604
OS: windows xp


Quote:
Originally Posted by Cache
You are making an array with 5 elements, not 4. Because array indexing starts at 0.

0,1,2,3,4 = 5 elements.
This is wrong. int a[4] would declare a array with 4 elements

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.
__________________
mgoldb2 is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-12-2005, 10:01 AM   #6 (permalink)
Registered User
 
Join Date: Dec 2005
Posts: 5
OS: XP


Quote:
Originally Posted by mgoldb2
This is wrong. int a[4] would declare a array with 4 elements

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.
How right you are. Doesn't inspire much confidence for my first post on this forum lol.

EDIT: Seems I can't edit my other post to correct it either.

Last edited by Cache; 12-12-2005 at 10:10 AM.
Cache is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-12-2005, 01:57 PM   #7 (permalink)
Registered User
 
Join Date: Dec 2005
Location: New England
Posts: 139
OS: Windows XP Home SP3

My System

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! ^_^/)
TheUnlimited is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-13-2005, 06:50 PM   #8 (permalink)
Registered User
 
Join Date: Dec 2005
Location: New England
Posts: 139
OS: Windows XP Home SP3

My System

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:
int n rand() % 50 100;  //bounded random number
int *mooPoint[n]; 
This little bit refuses to run. The random number won't be zero, and is a constant -- it's simply not hardcoded in.
PHP Code:
#include <iostream>
#include <ctime>

using namespace std;

int main()
{
    
srand(time(NULL));  //seeds the random generator

    
const int n rand() % 100 50;  //bounded random #

    
int *mooPoint[n];   //pointer array of size "const int n" defined by rand()

    
cout<<n<<endl;

    return 
0;


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...
TheUnlimited is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-16-2005, 09:33 PM   #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:
int main()
{
    
srand(time(NULL));  //seeds the random generator 

    
const int n rand() % 100 50;  //bounded random #

    
int moo_point =0;               // ready up your future-array 

    
moo_point = (int*) malloc(sizeof(int)) ;   //allocate memory base on ('n' * sizeof(int))

    
memset(moo_point,0, (nsizeof(int)));            // optionally initialize all array cells with 0's
                                                    
    
cout<<moo_point[0]<<endl// manipulate your array as much as you like :P, range is always 0 to n-1
    
cin >> moo_point[1];
    
free(moo_point);                    // free up the allocated memory after use
    
moo_point 0;                        // make the now obsolete pointer 'unusable'
    
return 0
(edit: put the code in php tags)

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.
Sesshou is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-19-2005, 09:57 PM   #10 (permalink)
Registered User
 
Join Date: Dec 2005
Location: New England
Posts: 139
OS: Windows XP Home SP3

My System

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. ^_^
TheUnlimited is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Old 12-20-2005, 09:47 AM   #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
Sesshou is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -7. The time now is 03:03 AM.



Copyright 2001 - 2009, Tech Support Forum
Home Tips Plus | Outdoor Basecamp | Automotive Support Forum

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85