Monday 15 February 2010

c - Function deque to array not working -



c - Function deque to array not working -

hello have problem. made function:

void* deque2array(tdeque * d){ void *arr = null; int i; tnodo * aux = d->ppio; for(i=0; < d->cant; i++){ arr = aux->elem; arr++; aux=aux->sig; } homecoming arr; }

then made tester sure function works right.

tdeque * queue = createdeque(); int x=5; int y=2; int z=3; insertindeque(queue, &x); insertindeque(queue, &y); insertindeque(queue, &z); int* pointer = deque2array(queue); int i; for(i=0; i<numberofelements(queue); i++){ pointer = pointer + i; printf(" %d ", *pointer); }

but memory address , don't know doing wrong.

you doing several things wrong. first, posting incomplete code example. don't know d->ppio, d->cant, aux->sig, or aux->elem are, nor know whether setting them in insertindeque.

in deque2array:

arr = aux->elem

you want:

*arr = aux->elem

otherwise, function returning value stored in lastly aux->elem + 1. it's hard fathom how run without exception if it's supposed homecoming void*.

in tester:

pointer = pointer +

you modifying pointer on every iteration of loop. pointer on nth iteration of loop original value of pointer plus sum of integers 0 n, not original value of pointer plus i appears intended. again, miracle if code runs without exception, since reading memory beyond bounds of array. given big plenty homecoming value of numberofelements(queue), guaranteed produce error.

once you've fixed these problems, if assume aux->elem think is, , insertindeque think does, new problem inserting addresses of x, y, , z structure, not values, create sense corrected test code print addresses of variables. of now, since reading undefined memory locations, results not going create sense regardless of in info structure.

c arrays void-pointers

No comments:

Post a Comment