quick question about initializing structs

Posted By: Reconnoiter

quick question about initializing structs - 10/27/15 12:07

Hi,

If I do MY_STRUCT* my_structarray[20]; after setting up MY_STRUCT, will my_structarray[20] be set automatically to NULL?
So the following will not give me memory errors right? (just want to be sure):

Code:
function my_function()
{
...
  if (my_structarray[0] != NULL)
  {
    than do blablabla
  }
...
}



It seems like it has been set to NULL here. Cause otherwise I will manually set it to NULL in a for loop.
Posted By: Florastamine

Re: quick question about initializing structs - 10/27/15 12:40

Yes.

Code:
#include <acknex.h>

typedef struct {
	int x;
	int y;
} data;

data *d;

int main()
{
	if( d == NULL ) printf("Caught!");
	
	return 0;
}



By the way, NULL is just a fancy name for (void *) 0 so you might want to do if(!my_structarray[0]) instead.
Posted By: Reconnoiter

Re: quick question about initializing structs - 10/27/15 12:57

tnx
Posted By: Reconnoiter

Re: quick question about initializing structs - 10/29/15 12:24

Hi,

Hmm this doesn't seem to work for me. In the code (2nd) below it should ignore empty places in my struct pointer with 2 arrays, but it doesn't (for some reason the struct pointer is not set to NULL), even when would manually set it to null:

Code:
...
MY_STRUCT* mystruct[20][20];

function struct_startup()
{
        //empty pointer first
	var i, j;
	for (i = 0; i < 20; i++)
	{  
		for (j = 0; j < 20; j++) mystruct[i][j] = NULL; 
	} 
	
	//than fill
        ...
}



no errors yet. And if I filter to struct that I have filled with real values it works fine to. But when I have set pointer to NULL only and switch to it through the code below; it gives an error (which it shouldn't cause it should detect that it is NULL & skip it in in the first place right??), the line with the break doesn't work well:

Code:
//store filter for 20 types
var filter[20] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

function next_filter (var buttonnumber, PANEL* p_panelofbutton)
{
  //set to current type & filter
  var type = p_panelofbutton.skill_x; //e.g. 0
  var filter = filter[type]; //e.g. 1

                //till struct that is not NULL
		while(1)
		{
			filter[type] += buttonnumber * 2 - 3; //either +1 or -1
			if (filter[type] > 19) filter[type] = 0;
			if (filter[type] < 0) filter[type] = 19;	
			
			if (mystruct[type][filter] != NULL) break; //it always breaks here for some reason
                }		
  
  refresh_page(); //refresh a window with debug vars & digits etc.
}

© 2024 lite-C Forums