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.
Probably what you read was simply assigning the memory address of the original char array to the copy. Something like:
Code:
char *original = "This is an original string.\0";
char *copy;
copy = original;
This doesn't actually copy anything more than the memory address. If you make changes to the char array in either spot, the changes will be reflected in both places, because they are sharing the memory address.
In order for your example to work without causing memory errors, you'll need to allocate enough space in the new location for the copy. Also, the string copy loop should be checking for a null terminator as it's basis for loop-termination.
It's also a good idea to include some length checking, to make sure that the copy's length doesn't exceed it's storage space allotment.
now q contains a copy of p. the best way to do it however is using the functions defined in string.h or cstring since it looks like your using c++. if you are happy using c++ then use the std::string class from stl its much better.
Also, the string copy loop should be checking for a null terminator as it's basis for loop-termination.
Originally posted by kev82 it is
It is? Are you sure? My tests prove otherwise, but if so, I'd like to understand how it's doing so, so that I may take advantage of it in the future. (ok, I just got it to compile, but I get the compiler warning "suggest parentheses around assignment used as truth value", which I'm not quite sure how to solve, since I've never used this method of string copying before)
Actually, that won't work, because it's trying to increment the memory address of a predefined char array. This is definitely not the same thing as a pointer to a char array. Upon trying to compile said code, I receive this error:
Code:
~> g++ -Wall -o test test.c
test.c: In function `int main()':
test.c:10: non-lvalue in increment
And, even if it did work, now both pointers are pointing to the ends of the strings (I think 1 char past the null terminator), and not the beginnings.
The code would need to take a pointer placeholder to q, and use it for incrementation, instead of the original.
Also, this loop still doesn't do any length checking (a big no-no in my book).
edit: the reason it works is the statement *c++=*d++ evaluates *d so when the null terminator is copied the expression evaluates to zero which automatically terminates the loop.
I thank all of you for your replies. I generally use C++ string functions, but I read about this type of copying in Stroustrup's "The C++ Programming Language", and I could not get it to work for me. I thought that I must have done something wrong, but it seems that the method by itself is wrong.
Did you read the replies ? The method while(*q++ = *p++) is perfectly fine. Think Stroustrup, a software engineer with years of C experience and a doctoral degree got it wrong ?
You just forgot to allocate memory for the copy. Use malloc or calloc when doing C, new[] in C++, or allocate it on the stack (fast, but beware of dangling pointers on a return).
If you really want to program in C or C++, you must understand the way C/C++ programs execute. Pointers and references are just numbers that reference an address in memory. Defining a pointer variable does not allocate memory for the stuff it points, unless you use an initializer.
IMHO this is a major problem with C++. You still have to know the low level details. I learned assembly way before C and C++, and did not have too much trouble. If you come from some high level background, you will have a lot to learn.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.