ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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"
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.