•
•
•
•
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
![]() |
•
•
Join Date: Aug 2007
Posts: 4
Reputation:
Rep Power: 0
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 **
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,160
Reputation:
Rep Power: 38
Solved Threads: 930
•
•
•
•
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.
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.
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
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- 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)



Linear Mode