Skip to main content
gamedev.net
🔒 Locked

pointer question

Started by Jesper T 5 replies 300+ views
Original Post
Jesper T
Jesper T
well this is the situation: I have two types of structs somat like this:
      
struct a_str {
  double array[4][4][3];
};

struct b_str {
  a_str *aptr;
  double *arrayptr[3];
};
  
// Next I make two structs of those kinds like this:

      
a_str a;
b_str b;

b.aptr = &a
  

// And here is where the problem comes in:

// I want to make the *arrayptr point to the array in the struct

// that b.aptr points to, I tried various things, like this: 


b.arrayptr[0] = &b.aptr->array[0][0][0];

// But it seems that it either makes a compile error, or it

// makes it crash (the one above makes the program crash).

// Note that it has to be pointed through the b.aptr, 

// not directly to a.array

  
Edit: just to clarify; *b.arrayptr[0/1/2] should point to a.array[0][0][0/1/2] respectivly Could any1 help out ? btw: Just wanna say big thanks to everyone here helping out solving problems, I've probably gotten help here on these boards over a hundred times. Keep up the good work Edited by - Jesper T on October 11, 2001 2:32:08 PM
kirkd
kirkd
It seems to me, and I''m sure I''ll be corrected if I''m wrong, that you are trying to set the pointer b.arraptr[0] to the quantity stored in the array b.aptr->array[0][0][0]

Try taking off the [0][0][0] at the end of the b.aptr->array and remember that the name of the array alone represents a pointer to the first element in the array.

In other words, try

b.arraptr[0] = b.aptr->array

-Kirk
Jesper T
Jesper T
Hmm nope, compile error, I also tried:

b.arrayptr[0] = b.aptr->array[0][0];

But that made the program crash.. damn, any other ideas ?

(edit: spelling errors)

Edited by - Jesper T on October 11, 2001 2:29:03 PM
Nytegard
Nytegard
This I know works. You're code seems alright from what I can see. You might have a problem somewhere else.

      
#include <iostream.h>

#include <conio.h>

struct a_str
{
double array[4][4][3];
};

struct b_str
{
a_str *aptr;
double *arrayptr[3];
};

void main()
{
a_str a;
b_str b;

double x = 0;

for(int aa = 0; aa < 4; aa++)
{
for(int bb = 0; bb < 4; bb++)
{
for(int cc = 0; cc < 3; cc++)
{
a.array[aa][bb][cc] = x;
x+=2;
}
}
}

b.aptr = &a

cout << a.array[3][2][1] << endl; // prints 86

cout << b.aptr->array[3][2][1] << endl; // prints 86


b.arrayptr[0] = &b.aptr->array[0][0][0];

b.arrayptr[0] += 41;
cout << *b.arrayptr[0] << endl; // prints 82

}


Edited by - Nytegard on October 11, 2001 3:01:36 PM
Jesper T
Jesper T
Hmmm it crashes if I do it the same way in my prog, I thought I had everything else commented out.. errr... well then I''ll have to go through it then, thank God there is a search option in msvc++ (its a quite large prog im makin lol)

Thanks for the help anyways

JT
Oluseyi
Oluseyi
quote:
Original post by Jesper T
just to clarify; *b.arrayptr[0/1/2] should point to
a.array[0][0][0/1/2] respectivly


This should work:
b.arrayptr = b.aptr->array[0][0]; 


(Assign an array to an array of equal dimensions.)
Jesper T
Jesper T
Yup, both

b.arrayptr = b.aptr->array[0][0];

and

b.arrayptr[0] = &b.aptr->array[0][0][0];
b.arrayptr[1] = &b.aptr->array[0][0][1];
b.arrayptr[2] = &b.aptr->array[0][0][2];

works now lol

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.