Copying arrays
hello-
i am trying to copy a char array into another char array. Is this possible just by assignment, or do i have to loop though and copy each value? Right now i am looping through, but i would prefer not to do that....
Thanks!
You can''t do it just by assignment.
However, you can do it using a call to memcpy() or if you know it is a nullterminated string you can use strcpy().
However, you can do it using a call to memcpy() or if you know it is a nullterminated string you can use strcpy().
By assigning a char array to another you copy the pointers to the array only, meaning the change you do in the first will occur in the other.
I suggest you read up on pointers before trying to copy arrays at all.
-----------------------------
Final Frontier Trader
I suggest you read up on pointers before trying to copy arrays at all.
-----------------------------
Final Frontier Trader
an array of characters can be copied using the
strcpy function.
char *1="This is a string";
char *2;
strcpy(2,1);
or
char string[80];
strcpy( string, "Another example " );
or
char string[80];
char string2[]="This is another string";
strcpy( string, string2 );
I do agree that if you want to learn c++ at some time you need to learn about pointers, but why if a person asks for a function to copy character arrays do you tell him to learn about pointers?
strcpy function.
char *1="This is a string";
char *2;
strcpy(2,1);
or
char string[80];
strcpy( string, "Another example " );
or
char string[80];
char string2[]="This is another string";
strcpy( string, string2 );
I do agree that if you want to learn c++ at some time you need to learn about pointers, but why if a person asks for a function to copy character arrays do you tell him to learn about pointers?
also
char string1[]="This is a string";
char* string2;
string2=string1;
is acceptable, and will copy by operator.
BUT char string2[20];will not copy directly by the operator, because the arrays are of different size.
char string1[]="This is a string";
char* string2;
string2=string1;
is acceptable, and will copy by operator.
BUT char string2[20];will not copy directly by the operator, because the arrays are of different size.
quote:Original post by RhoneRanger
I do agree that if you want to learn c++ at some time you need to learn about pointers, but why if a person asks for a function to copy character arrays do you tell him to learn about pointers?
So that he doesn''t do things like this:
quote:Original post by RhoneRanger
char *1="This is a string";
char *2;
strcpy(2,1);
And give himself a harder time than necessary .
quote:
char string1[]="This is a string";
char* string2;
string2=string1;
Is char* string2; a pointer? If so what kind of pointer?
By the way does anyone know a good place that tells you all about pointers? I have a learn C in 24 hours book and I understand every concept it has about pointers but it doesnt seem to be enough to understand more complex uses of pointers.
I searched with Google.. for example try this:
http://www.codeproject.com/useritems/PointerPrelude.asp
and if you want a book about c/c++ I suggest you the ONLY ONE: the Stroustrup!
Regards.
Fire burn wisdom in me,
Wisdom set mind and spirit free,
Moonlight shows me the mysteries of life,
Winternight gives me clearsight and storms to fight.
http://www.codeproject.com/useritems/PointerPrelude.asp
and if you want a book about c/c++ I suggest you the ONLY ONE: the Stroustrup!
Regards.
Fire burn wisdom in me,
Wisdom set mind and spirit free,
Moonlight shows me the mysteries of life,
Winternight gives me clearsight and storms to fight.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement