string copy

B

bml

char s1 = "this string";

char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);

/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

/* Why empty content of s2 print out here*/

Thanks a lot!
 
A

Artie Gold

bml said:
char s1 = "this string";

char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);

/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

/* Why empty content of s2 print out here*/

Hint: What is s2 now pointing to?

[BTW -- in the future, post *real* code (the most minimal possible
runnable snippet); otherwise we're just guessing.]

HTH,
--ag
 
M

Martin Ambuhl

bml said:
char s1 = "this string"; ^^ missing '*'

char *s2;
s2 = (char *)malloc(30);
^^^^^^^ silly cast
No error check
memset(s2, 0, 30);
^^^^^^^^^^^^^^^^^
useless function call
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

/* Why empty content of s2 print out here*/
You were unlucky; your computer should have exploded.
Because s2 points beyond the end of the string. What did you think was
happening when you incremented it?
Thanks a lot!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(void)
{
char *s1 = "this string";
char *t1 = s1;
char *s2;
char *t2;
if (!(s2 = malloc(strlen(s1) + 1))) {
fprintf(stderr, "malloc failed for s1\n");
exit(EXIT_FAILURE);
}
t2 = s2;
while ((*t2++ = *t1++)) ;
printf("s1: \"%s\"\ns2: \"%s\"\n", s1, s2);
return 0;
}
 
T

Thomas Pfaff

bml said:
char s1 = "this string";

I assume this is a typo and you meant to write

char *s1 = "this string";
char *s2;
s2 = (char *)malloc(30);

Don't cast malloc. It is not necessarry and may hide a serious error
memset(s2, 0, 30);

Make sure malloc succeeded before accessing s2.

if (s2 != NULL)
/* use s2 here */

If you simply want to "empty" the string then *s2 = '\0' will do.
/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

Why not simply use strcpy?
/* Why empty content of s2 print out here*/

Where does s2 point after the loop completes? You'll need to remember
the original pointer,

char *t = s2;
while (*s2++ = *s1++);
s2 = t;
printf ("%s\n", s2);
 
J

Jarno A Wuolijoki

Where does s2 point after the loop completes? You'll need to remember
the original pointer,

char *t = s2;
while (*s2++ = *s1++);
s2 = t;

char *t = s2;
while (*t++ = *s1++) ;

If this seems like a mindless nit I'm probably just overly biased against
the idiom of modifying a variable and then popping it back..
 
T

Thomas Matthews

bml said:
char s1 = "this string";

const char * s1 = "this string";
Always treat string literals as constants. Thus
s1 is declared as a pointer to constant data.
Constant meaning it won't change.

char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);

char * s2;
if (strlen(s1) < 30)
{
s2 = malloc(30);
}
else
{
s2 = malloc(strlen(s1) + 1);
}
if (s2 == NULL)
{
/* error processing */
}

/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

strcpy(s2, s1); /* copy string s1 to s2 */

/* Why empty content of s2 print out here*/

puts("String s2:");
puts(s2);
Thanks a lot!



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
E

Emmanuel Delahaye

bml said:
char s1 = "this string";

char *s2;
s2 = (char *)malloc(30);
memset(s2, 0, 30);

/* this supposes to copy string s1 to s2 */
while((*s2++ = *s1++) != '\0');

/* Why empty content of s2 print out here*/

Empty ? I'm not sure. I would have said 'undeterminated', due to the lack of
final 0.

add :
*s2 = 0;

but you are making a design mistake. You have changed the value of s2, hence
you have lost it's initial value:

- The string copy was somehow correct, but you don't know any more where does
the copied string begin.
- You are now unable to free the allocated block which has created a memory
leak. (A Bad Thing (c))

If you want to make a copy of a string, I recommend to desing your own
'strdup()' function:

#include <string.h>
#include <stdlib.h>

char *str_dup (char const * const s)
{
char *sdup;

if (s != NULL)
{
size_t size = strlen (s) + 1;

sdup = malloc (size);

if (sdup != NULL)
{
memcpy (sdup, s, size);
}
}
return sdup;
}
 
A

Al Bowers

Emmanuel said:
If you want to make a copy of a string, I recommend to desing your own
'strdup()' function:

#include <string.h>
#include <stdlib.h>

char *str_dup (char const * const s)
{
char *sdup;

To correct the flaw in the return value should the argument, s,
be a NULL value, make this:

char *sdup = NULL;
 
P

Peter Pichler

Emmanuel Delahaye said:
Empty ? I'm not sure. I would have said 'undeterminated', due to the lack of
final 0.

That's probably why he added the otherwise useless memset() in the first
place ;-)
 
B

Bliss

I would have written:
while(*s2++ = *s1++);It does not lack the terminating 0
Look at what happens in the while loop--
*s1 is copied to *s2 before the test for *s1!=0
When *s1 is zero that zero is assigned to *s2 and _then_ the loop
stops.
That's probably why he added the otherwise useless memset() in the first
place ;-)
Roger
 
P

Peter Pichler

Bliss said:
I would have written:
while(*s2++ = *s1++);
It does not lack the terminating 0
Look at what happens in the while loop--
*s1 is copied to *s2 before the test for *s1!=0

....after which, s1 and s2 get incremented, pointing beyond the terminating
0.
When *s1 is zero that zero is assigned to *s2 and _then_ the loop
stops.

....leaving both s1 and s2 pointing to a cabbage field.

Ther OP had probably been experiencing these problems and insted of trying
to find the problem, he "fixed" it by adding the useless memset().

Peter
 
E

Emmanuel Delahaye

In 'comp.lang.c' said:
To correct the flaw in the return value should the argument, s,
be a NULL value, make this:

char *sdup = NULL;

Absolutely. (Too fast typing).
 
T

Thomas Matthews

Peter said:
Why do you need the first branch?

Just to show the OP that the string overflowed the original
allocated length.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top