C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 27th, 2008, 01:11 AM
angel.white angel.white is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Kansas
Posts: 17 angel.white User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 40 m 44 sec
Reputation Power: 0
ANSI C: Array element swapper

Hello, I am trying to write a function which swaps values. This will be used in a sorter I am writing for a school project.

Currently I can sort by swapping the actual values, ie if
a and b are integers, I can swap them with this code:

Code:
void swap( int *a, int *b)
{
int c;
c=*a;
*a=*b;
*b=c;
}


But that only works for nice simple number arrays. Now I need to apply this to an array list, where the elements are structures and not integers.

I realized that if I can simply swap what each element points to, then I can do this very easily.
ie my_struct[1] points to structure_a
and my_struct[2] points to structure_b

then if I can swap it so that it now goes like this:
my_struct[1] points to structure_b
my_struct[2] points to structure_a

I can easily swap elements in the array of structures without lengthy tedious copying of values, and instead simply redirect pointers.

However, I am having difficulty figuring out how to perform this task, and was hoping someone could help me by modifying the piece of code I wrote so that it worked in the manner I am shooting for. Then I will be able to see how it works, and adjust it appropriately to fit the program that I am writing.

Thank you.

Reply With Quote
  #2  
Old February 27th, 2008, 06:43 AM
Annie79's Avatar
Annie79 Annie79 is offline
Meow Black Belt
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2005
Location: Beaverton OR
Posts: 932 Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)Annie79 User rank is Colonel (50000 - 60000 Reputation Level)  Folding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner FolderFolding Points: 49426 Folding Title: Beginner Folder
Time spent in forums: 4 Weeks 1 Day 1 h 36 m 24 sec
Reputation Power: 525
Is this what you want?
c Code:
Original - c Code
  1.  
  2. void swap(struct my_struct* a, struct my_struct* b) {
  3.     struct my_struct* c;
  4.     c = a;
  5.     a = b;
  6.     b = c;
  7. }
__________________

Last edited by Annie79 : February 27th, 2008 at 06:52 AM.

Reply With Quote
  #3  
Old February 27th, 2008, 11:43 AM
angel.white angel.white is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Kansas
Posts: 17 angel.white User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 40 m 44 sec
Reputation Power: 0
Quote:
Originally Posted by Annie79
Is this what you want?
c Code:
Original - c Code
  1.  
  2. void swap(struct my_struct* a, struct my_struct* b) {
  3.     struct my_struct* c;
  4.     c = a;
  5.     a = b;
  6.     b = c;
  7. }
Thank you, but that doesn't seem to work. That seems to correctly redirect the pointers in the swap() function, but not the pointers that was sent to it.

if we pretend there is a line of yarn from
int1 and int2, to the memory that holds their values, then I want the program to clip the ends of the line of yarn, and move it over to the other address. So each variable now points to the thing in memory that the other guy used to point to. This way I don't have to change any data at all.

I'm not sure if this can be done, because I don't really know how arrays work. If they are just the next consecutive spot in memory, then I don't think my plan will work, because if I reorder what element points to what memory, they won't be consecutive anymore, and the array won't be able to get from one element to the next :/

But I'm hoping that isn't the case.

Anyway, thank you for spending time on it, even though it wasn't exactly what I was looking for.

Reply With Quote
  #4  
Old February 27th, 2008, 11:56 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,345 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 38 m 38 sec
Reputation Power: 54
Code:
#include <stdlib.h>
#include <string.h>

void swap(void *a, void *b, size_t element_sz)
{
	unsigned char *tmp=malloc(element_sz);
	memcpy(tmp, a, element_sz);
	memcpy(a, b, element_sz);
	memcpy(b, tmp, element_sz);
	free(tmp);
}

int main()
{
	typedef
	struct
	{
	   int a;
	   char b[4];
	} mystruct_t;

	mystruct_t a={1,"aaa"};
	mystruct_t b={2,"bbb"};
	printf("a=%d %s b=%d %s\n",a.a, a.b, b.a, b.b);
	swap(&a, &b, sizeof(mystruct_t));
	printf("a=%d %s b=%d %s\n",a.a, a.b, b.a, b.b);
	return 0;
}


output:
Quote:
/home/jmcnama> cc t.c
/home/jmcnama> a.out
a=1 aaa b=2 bbb
a=2 bbb b=1 aaa
Comments on this post
Annie79 agrees!
angel.white agrees!

Reply With Quote
  #5  
Old February 27th, 2008, 01:43 PM
jnsg jnsg is offline
JackOfAllTrades, MasterOfNone
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Kentucky, USA
Posts: 398 jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level)jnsg User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 4 h 43 m 27 sec
Reputation Power: 123
Send a message via AIM to jnsg
Altering Annie79's code slightly:
c Code:
Original - c Code
  1. void swap(struct my_struct_t** a, struct my_struct_t** b) {
  2.     struct my_struct_t* c;
  3.     c = *a;
  4.     *a = *b;
  5.     *b = c;
  6. }

Then you can pass pointers to the array elements:
c Code:
Original - c Code
  1. /* assuming my_struct is an array of my_struct_t */
  2. swap(&(my_struct[1]), &(my_struct[2]));

If you were doing this in C++, I would say to pass by reference; this is essentially the C version of that.
Comments on this post
angel.white agrees!

Last edited by jnsg : February 28th, 2008 at 01:10 PM.

Reply With Quote
  #6  
Old February 27th, 2008, 10:35 PM
angel.white angel.white is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Location: Kansas
Posts: 17 angel.white User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 40 m 44 sec
Reputation Power: 0
Thank you everybody for all your help, I've been able to successfully get the swap function to work ^_^

Now my whole sorter works, and I can move onto the next stage in my project.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > ANSI C: Array element swapper

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap