User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,821 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,013 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 444 | Replies: 2 | Solved
Reply
Join Date: Aug 2007
Posts: 4
Reputation: xyster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xyster xyster is offline Offline
Newbie Poster

swap function - generic use?

  #1  
May 17th, 2008
I have a quick question that is been bugging me.

void
swap(void *a, void *b)
{
     void *tmp;
     tmp = a;
     a = b;
     b = tmp;
}

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 **
struct heap_s
{
     heap left, right;
     void* data;
};

struct event_s {
     etime       time;
     int     e_num;
     void        (*event_fn)(event,  pqueue);
     void        *details;
     };
Last edited by xyster : May 17th, 2008 at 6:24 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,160
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: swap function - generic use?

  #2  
May 17th, 2008
Originally Posted by xyster View Post
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.

void swap(void** a, void** b)
{
    void* temp = *a;
     *a = *b;
      *b = temp;
}

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

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 8:48 am.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Reply With Quote  
Join Date: Aug 2007
Posts: 4
Reputation: xyster is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
xyster xyster is offline Offline
Newbie Poster

Re: swap function - generic use?

  #3  
May 17th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:19 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC