Global variable array pointer C programming -
i'm working on project need array of pointers structs. have created global variable functions manipulate array hold pointer array can access functions. however, i'm running problems pointer just...changes , isn't pointing right thing anymore.
i create array so:
void initpqueue() { eventptr pqueue[qsize]; int i; float t; for(i = 1; < qsize; i++) { t = getnextrandominterval(getl()); pqueue[i] = createevent(t); } setpqueue(pqueue); buildpqueue(); }
i use setpqueue(pqueue) set global variable....like so...
void setpqueue(eventptr* pqueue) { pqueueptr = pqueue; }
the global variable declared as:
eventptr* pqueueptr;
here struct: (in .h file.. atm)
struct event { float etime; double stime; int status; }; typedef struct event event; typedef struct event* eventptr;
everything awesome till point. buildpqueue works right... using pqueueptr .... however...i went make test functions output pqueue array , got ugly...
void outtest() { int i; printf("\n\n"); for(i = 0; < qsize; i++) { if(pqueueptr[i] != null) printf("%f ", pqueueptr[i]->etime); else printf("null "); } }
this gives me output pointer array contains null values when not... function , last 2 in same file. put loop in setpqueue , worked fine... should in outtest...
i don't understand why this... i've come asking experts... :)
any great... :)
in initpqueue(), pqueue allocated on stack; function returns, memory not valid anymore.
to have data survive function call, need allocate on heap using malloc() (or other memory allocation function).
Comments
Post a Comment