|
quick question about initializing structs
#455679
10/27/15 12:07
10/27/15 12:07
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
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):
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.
Last edited by Reconnoiter; 10/27/15 12:08.
|
|
|
Re: quick question about initializing structs
[Re: Reconnoiter]
#455680
10/27/15 12:40
10/27/15 12:40
|
Joined: Apr 2015
Posts: 20 Vietnam
Florastamine
Newbie
|
Newbie
Joined: Apr 2015
Posts: 20
Vietnam
|
Yes.
#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.
|
|
|
Re: quick question about initializing structs
[Re: Reconnoiter]
#455765
10/29/15 12:24
10/29/15 12:24
|
Joined: Dec 2011
Posts: 1,823 Netherlands
Reconnoiter
OP
Serious User
|
OP
Serious User
Joined: Dec 2011
Posts: 1,823
Netherlands
|
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:
...
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:
//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.
}
Last edited by Reconnoiter; 10/29/15 12:33.
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|