| | |
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 7: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 9:48 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
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)
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf scripting segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






