Array of pointers to structs // C++

Hello!

I have a problem with an array of pointers to structs. I define a struct, create an array of pointers to that struct, store some variables through a function and, when I check those vars from inside the function, they are correctly stored, but when I check it later using (mcSimpleButtonArray[i])->w , I always get 0. This is the code

  
  
// Create a simple struct  
struct mcSimpleButton {  
    int x,y,w,h;  
};  
  
// Create an array of pointers to the struct mcSimpleButton  
mcSimpleButton *mcSimpleButtonArray[NUMBUTTONS];  
  
// Initialize the pointers for later checks? Not sure about this...  
for(int i=0;i<NUMBUTTONS;i++){  
    mcSimpleButtonArray[i] = 0;  
}  
  
// Create one mcSimpleButton  
mcSimpleButton onOff;  
  
// Calling the function to store some vars in the mcSimpleButton  
registerSimpleButton(400, onOff);  
  
// This is the function  
void registerSimpleButton(int w, mcSimpleButton nom){  
    nom.w=w;  
    // THIS PRINTS THE CORRECT VALUE OF W: 400, so its value is correctly stored  
    printf ("H %i\n", nom.h);  
}  
  
//HERE IT COMES THE PROBLEM, I ALWAYS GET w=0   
//ALTHOUGH PREVIOUSLY I COULD CHECK THAT IT WAS STORED CORRECTLY  
for(int i=0;i<NUMBUTTONS;i++){  
    if(mcSimpleButtonArray[i] != 0){  
        int showMeYourW = (mcSimpleButtonArray[i])->w;  
        printf("showMeYourW %i \n", showMeYourW);  
    }  
}  

I am not sure if (mcSimpleButtonArray[i])->w is the correct way to access to that variable. I’m very new to all this so it’s possible that I’m doing something totally wrong :stuck_out_tongue:

Thanks!

Hi

when you pass somethin to a function as you are doing in:

  
// Create one mcSimpleButton  
mcSimpleButton onOff;  
  
// Calling the function to store some vars in the mcSimpleButton  
registerSimpleButton(400, onOff);   

you’re indeed passing a copy, so inside the function you modify the copy that get’s deleted when the function returns, in order to make that works, you have to define registerSimpleButton as:

  
// This is the function  
void registerSimpleButton(int w, mcSimpleButton * nom){  
    nom->w=w;  
    // THIS PRINTS THE CORRECT VALUE OF W: 400, so its value is correctly stored  
    printf ("H %i\n", nom->h);  
}  
  

and call it:

  
registerSimpleButton(400, &onOff);  

That way instead of passing a copy you’re passing a pointer to the original variable so when the function returns your variable is already modified.

Hello Arturo

Thanks for your reply, but I have tried it and works as it did before… but now I realize that I forgot a line of code when was cleaning it to post it in the forums :oops:

The registerSimpleButton function, already modified with your suggestions and the missing line looks like this:

  
void registerSimpleButton(int w, mcSimpleButton nom){  
    nom.w=w;  
    printf ("W %i\n", nom.w);  
    mcSimpleButtonArray[lstB] = (mcSimpleButton*) calloc(1, sizeof(mcSimpleButton));  
}  
// lstB is an incremental variable  
  

As I understand, I assign to the lstB position of the array a pointer to the memory block allocated for every mcSimpleButton that I “register”

Could the problem be there?

Thanks!

Sorry I thought that you were trying to modify onOff, not a position on the array. Looking at your code you’re not modifying the array, the function must be somehing like:

  
void registerSimpleButton(int w, mcSimpleButton nom){  
    nom.w=w;  
    printf ("W %i\n", nom.w);  
    mcSimpleButtonArray[lstB] = (mcSimpleButton*) calloc(1, sizeof(mcSimpleButton));  
    mcSimpleButtonArray[lstB] ->w=w;  
}  
// lstB is an incremental variabl  

Also:

  • I don’t understand very well why are you passing the onOff variable to the function if you’re trying to modify the array

  • Don’t use calloc is really old, better:

  
mcSimpleButtonArray[lstB] = new mcSimpleButton;  

Now it’s working!

I’ll take a deep look to try to better understand it. Now this is becoming fun :smiley:

Gràcies!!

I’m not really sure exactly what you’re trying to do, but there are a number of things I would do differently. One in particular catches my attention since it seems to be a common thing in OF-land (I think because of a commonly referenced tutorial). This is the array of pointers. Almost every use of this construct I’ve seen on these boards has been unnecessary and tends to create extra complexity. Why not just allocate an array object objects and have them accessible through a single pointer?? Doing it with an array of pointers to objects adds an extra level an indirection thus complication iteration and access code. Here’s how I would write your code from the information given in this thread:

  
  
  
// Create a simple struct  
struct mcSimpleButton {  
public:  
    int x,y,w,h;  
      
    mcSimpleButton()  
    : x(0), y(0), w(10), h(10)  
    {}  
  
    ~mcSimpleButton() {}  
};  
  
// Create an array of pointers to the struct mcSimpleButton  
mcSimpleButton mcSimpleButtonArray[NUMBUTTONS];  
  
// Create one mcSimpleButton  
mcSimpleButton onOff;  
  
// Calling the function to store some vars in the mcSimpleButton  
registerSimpleButton(400, onOff);  
  
void registerSimpleButton(int w, mcSimpleButton &nom){  
    nom.w=w;  
    printf ("W %i\n", nom.w);  
}  
// lstB is an incremental variable   
  
//HERE IT COMES THE PROBLEM, I ALWAYS GET w=0  
//ALTHOUGH PREVIOUSLY I COULD CHECK THAT IT WAS STORED CORRECTLY  
for(int i=0;i<NUMBUTTONS;i++){  
	int showMeYourW = mcSimpleButtonArray[i].w;  
	printf("showMeYourW %i \n", showMeYourW);  
}