943,840 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 2385
  • C RSS
May 17th, 2008
0

swap function - generic use?

Expand Post »
I have a quick question that is been bugging me.

  1. void
  2. swap(void *a, void *b)
  3. {
  4. void *tmp;
  5. tmp = a;
  6. a = b;
  7. b = tmp;
  8. }

I wrote this intending it to be either able to swap a structure, or just the data defined within a structure.

Will the swap function be able to do this, because.. when i tested it, it seemed to fail. but i didnt except it too.

it should atleast work for swapping the data stored within the structure though, shouldnt it? (the data is of type event, which is shown below the heap struct).


** here are the structures **
  1. struct heap_s
  2. {
  3. heap left, right;
  4. void* data;
  5. };
  6.  
  7. struct event_s {
  8. etime time;
  9. int e_num;
  10. void (*event_fn)(event, pqueue);
  11. void *details;
  12. };
Last edited by xyster; May 17th, 2008 at 7:24 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xyster is offline Offline
11 posts
since Aug 2007
May 17th, 2008
0

Re: swap function - generic use?

Click to Expand / Collapse  Quote originally posted by xyster ...
I wrote this intending it to be either able to swap a structure, or just the data defined within a structure.

Will the swap function be able to do this, because.. when i tested it, it seemed to fail. but i didnt except it too.
It fails because all it does is swap the pointers within the swap() function itself because the pointers are passed by value, not by reference.

  1. void swap(void** a, void** b)
  2. {
  3. void* temp = *a;
  4. *a = *b;
  5. *b = temp;
  6. }

Now the above will work ONLY if you pass a pointer to a pointer. It will also fail if you pass a pointer to some object.
int main()
{
    int a = 1;
    int b = 2;
    swap(&a, &b); // <<<<<<< wrong

    int* c = &a;
    int* d = &b;
    swap(&c, &d); // <<< OK
// when the swap returns, pointer c will point to b and pointer d will point to a.
// The value of a and b are still the same, only the two pointers
// were swapped

Quote ...
it should atleast work for swapping the data stored within the structure though, shouldnt it?
No.
Last edited by Ancient Dragon; May 17th, 2008 at 9:48 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
May 17th, 2008
0

Re: swap function - generic use?

oh, i see what i was doing wrong.

thanks for the quick reply.

i have added the updates and passed it the memory address now as you showed.

you can add the lovely "Solved" heading to my topic. Thankyou so much for such a quick reply.

This board has been really useful.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
xyster is offline Offline
11 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: IPC repeated attachment of shared memory segment at a fixed address.
Next Thread in C Forum Timeline: Transpose matrix (w\ pointers)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC