| | |
swap function - generic use?
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 11
Reputation:
Solved Threads: 0
I have a quick question that is been bugging me.
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 **
C Syntax (Toggle Plain Text)
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 **
C Syntax (Toggle Plain Text)
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.
•
•
•
•
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.
C Syntax (Toggle Plain Text)
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?
Last edited by Ancient Dragon; May 17th, 2008 at 8:48 am.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
![]() |
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Function templates and argument passing (C++)
- How to install slackware (Not as easy as MEPIS) (Getting Started and Choosing a Distro)
- muliplying without using a * operator! (C++)
Other Threads in the C Forum
- Previous Thread: IPC repeated attachment of shared memory segment at a fixed address.
- Next Thread: Transpose matrix (w\ pointers)
Views: 1223 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C
#include api array arrays binary binarysearch bit c++ calling char character code coke command conversion convert copy database decimal directory directorystructure drawing dude dynamic ebook error exec executable factorial fgets file fork framework free function functions givemetehcode givemetehcodez grade graphics gtkgcurlcompiling haiku help|help|help|help homework i/o input insert int integer intmain() lazy libcurl line linked linkedlist linux list lists loop lowest malloc matrix memory mysql no-effort output parallel path permutations pointer pointers problem process profile program programming read recursion recursive recv reverse scanf segmentationfault socketprograming spoonfeeding stack string strings strtok structures student system testing turbo-c turboc unix user variable windows _getdelim






