Archived

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

Arrays, references, and pointers

This topic is 6357 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

This is a repost, but the original question seems to have been buried, and was poorly worded. (read:confusing?) I have a large array of integers. I have another class that holds references to 3 elements of an integer array. I want to be able to pass 3 elements of the large array to an object of the class' type, and be able to have changes in the large array be reflected in the appropriate elements of class' array. To make it simpler:
        
//Here's a class that holds the first array:

class test
{
 private:
 // some other members

 public:
 int (&array1)[3];
 test(int (*newvals)[3]) : array1(*newvals) {}
 // some more members

};

void testproblem()
{
 int largearray[5];
 for (int t = 0; t < 5; t++) largearray[t] = 0;
 // create an object named 'testclass' of type test, with largearray elements 1, 3, 4
 // If I now change largearray[4] to 3,
 // (largearray[4] == testclass.array[2]) should return 1
}
        
I've been trying to get around this for a few days now - any suggestions? Thanks, -Medgur Edited by - Medgur on 7/17/00 3:10:14 PM

Share this post


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

int main()
{
int largearray[10];
int *smallarray[3];

for (int i = 0; i < 3; i++)
{
smallarray<i> = &largearray[i];
}
for (i = 0; i < 10; i++)
{
largearray[i] = 0;
}
largearray[2] = 10;

printf("largearray[2] = %d\n", largearray[2]);
printf("smallarray[2] = %d", *smallarray[2]);

return 0;
}


Is this what you wanted?!?!

-------------------------------------
That's just my 200 bucks!

..-=gLaDiAtOr=-..

Edited by - Gladiator on July 17, 2000 3:40:48 PM

Share this post


Link to post
Share on other sites
Thanks, but not quite, integrating it with what I was trying to perform, I get this:

                
class test
{
public:
int (&array1)[3];
test(int (*newvals)[3]) : array1(*newvals) {}
};

void testproblem()
{
int largearray[5];
int (*temparray)[3];
for (int t = 0; t < 5; t++) largearray[t] = 0;
temparray<0> = &largearray[1];
temparray<1> = &largearray[3];
temparray<2> = &largearray[4];
tclass = new test(temparray);
if (tclass.array1[0] == largearray[1]) cout << "It worked";
else cout << "It didn't work.";
}


But "ANSI C++ forbids comparison between pointer and integer"

Thanks,
-Medgur

Edited by - Medgur on July 17, 2000 4:07:42 PM

Edited by - Medgur on July 17, 2000 4:10:28 PM

Edited by - Medgur on July 17, 2000 4:11:17 PM

Share this post


Link to post
Share on other sites
Hrm. Doesn't seem that ANSI C++ allows for this sort of thing to take place, thanks anyways.

-Medgur

Edited by - Medgur on July 18, 2000 1:17:26 PM

Share this post


Link to post
Share on other sites
You''re declaring function pointers. Is that what you reall want to accomplish? Or are you supposed to work with integer pointers? Not sure what you''re trying to do.

-------------------------------------
That's just my 200 bucks!

..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Why would you want to compare a pointer and an integer?

You do realise that an array is actually a pointer to the first element? The [] operator simply offsets from that first element.

What are you using <> for ????

What are you trying to do? It doesn''t make sense ?!

Share this post


Link to post
Share on other sites