LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-26-2006, 05:48 AM   #1
zefram
Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 42

Rep: Reputation: 15
c swapping char* in function


Hi,

Say I have two array of chars:

char a[] = "one";
char b[] = "two";

And two pointers to them:

char *aptr = &a[0];
char *bptr = &b[0];

How should the swap(aptr,bptr) or swap(&aptr, &bptr) look like?

Have tried looking for an answer, and according to this:

http://www.linuxquestions.org/questi...d.php?t=434998

it has something to do with a pointer to a pointer.


This function almost does what I want, but for some reason (that I don't understand) it seg faults:

void swap(char *a, char *b) {
char t[] = "";
char *c = &t[0];
memmove(c,a,strlen(a)+1);
memmove(a,b,strlen(b)+1);
memmove(b,c,strlen(c)+1);
}

And i call it with swap(aptr,bptr);

 
Old 08-26-2006, 05:52 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Ubuntu/WSL
Posts: 9,788

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by zefram

This function almost does what I want, but for some reason (that I don't understand) it seg faults:

void swap(char *a, char *b) {
char t[] = "";
char *c = &t[0];
memmove(c,a,strlen(a)+1);
memmove(a,b,strlen(b)+1);
memmove(b,c,strlen(c)+1);
}

And i call it with swap(aptr,bptr);

Well, you are copying a string to a potentially read-only location with a size large enough to hold an empty string, two good reasons for a crash !
 
Old 08-26-2006, 06:00 AM   #3
zefram
Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 42

Original Poster
Rep: Reputation: 15
thanx, now it works!!

Wasn't thinking about that at all.

This is probibly why my other attempts at this didn't work either.
 
Old 08-26-2006, 06:18 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
It may "work", but does it do what you want it to? the memmove swaps the data pointed to, not the pointers themselves. "&a[0]" is equavalent to "&*a", "a" and "aptr" have the same value. If you haven't done so already, I'd suggest you get yourself a copy of K&R "The C Programming Language"
 
Old 08-26-2006, 06:47 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,599
Blog Entries: 4

Rep: Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905Reputation: 3905
Here's what I do ... after (ahem...) years I still do ...

Get out a piece of paper and write down what you want to have happen. Draw a box: that's your buffer. Draw another box: the second buffer. A couple more boxes: the two pointers. Arrows: what they point to.

The syntax is cryptic and it's very easy to get confused. The same strategy works in debugging: work out, line by line, what the code is actually doing, and draw a picture.

It works.

The routine as you intended to write it would need to allocate temporary storage to hold the value. It should also have some idea of just how large the buffers are: if you blindly swapped a 10-byte value into a 5-byte buffer, you'd be hosed.

Please... use C++ instead. That language has a "real" string-type and it is very efficiently implemented. There is a very good reason to use the most powerful and least error-prone tools available, and that reason is your time (and, your hair!). And, let the record show that I use both languages almost daily. Noodling with strings that way and worrying about overruns may have made sense in the 1970's (well, it did, actually... ): it doesn't now. Got power tools? Use power tools!

Last edited by sundialsvcs; 08-26-2006 at 06:54 PM.
 
Old 08-27-2006, 01:20 AM   #6
zefram
Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 42

Original Poster
Rep: Reputation: 15
Unfortunatly it must be in c (not c++). I would have done it in python or java if I had a choice.

Last night I started worying about garbage collection.

That supposed swap function of mine, creates a new char[], and points an old pointer to that. But what happens to the old char[], that the old pointer does not point anymore?
 
Old 08-27-2006, 01:28 AM   #7
zefram
Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 42

Original Poster
Rep: Reputation: 15
Oh, I missed your post tuxdev, I do need a good C book yes. Will look for that one, thanx.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C : Function to modify pointer to char introuble Programming 2 06-21-2006 12:24 PM
c char* pointer function question true_atlantis Programming 9 04-14-2006 01:29 PM
If I get invalid conversion from `const char*' to `char' what should I be lookin for? RHLinuxGUY Programming 5 03-12-2006 10:35 PM
bash cripting function to "identify" any char in a string? minike Programming 8 02-01-2006 09:45 AM
help passing char* to function call PTBmilo Programming 4 03-16-2003 07:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration