• Advertisement

Archived

This topic is now archived and is closed to further replies.

Why Doesn't this Work?

This topic is 5825 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream.h> #include <string.h> #include <stdlib.h> void strcopy(char *str1, char *str2); main() { char str[] = "Use malloc() to allocate memory."; char *ptr_str; int result; ptr_str = malloc(strlen(str) + 1); if (ptr_str != NULL) { strcopy(str, ptr_str); cout << "The String pointed to by prt_str is: "; ptr_str; cout << "\n"; result = 0; } else{ cout << "Malloc() function failed.\n"; result = 1; } return result; } void strcopy(char *str1, char *str2) { int i; for (i=0; str1; i++) str2[i] = str1[i]; str2[i] = ''\0''; }

Share this post


Link to post
Share on other sites
Advertisement
first thing that comes to mind is that you have a ; before ptr_str in your cout.
that would be the same as if you wrote:
cout << "The String pointed to by prt_str is: ";
ptr_str;

I'd say get rid of that ; after the "
second...I think you want "for (i = 0;i==strlen(str1);i++)"
or something like that...either that or i
I always get a little confused with the 0 based stuff...

-Lohrno

edit: ack I didnt need all the quote I had heh


Edited by - Lohrno on February 11, 2002 10:07:52 PM

Edited by - Lohrno on February 11, 2002 10:08:18 PM

Share this post


Link to post
Share on other sites
quote:
Original post by Lohrno
I''d say get rid of that ; after the "


And replace it with <<, of course:

cout << "The String pointed to by prt_str is: " << ptr_str;

And what''s wrong with the strcpy() function??


Share this post


Link to post
Share on other sites
quote:
Original post by Lohrno
second...I think you want "for (i = 0;i==strlen(str1);i++)"
or something like that...either that or i


I suggest you avoid using a function evaluation in a loop condition like
  i<strlen(str1)   
. Just assign the length of the string before the loop and use the value in the condition.
  
str_length = strlen(str1);
for(i=0;i<str_length;i++)
{

}

Faster code



Edited by - clabinsky on February 11, 2002 10:32:26 PM

Edited by - clabinsky on February 11, 2002 10:34:44 PM

Share this post


Link to post
Share on other sites
Another way:

void strcopy(char *str1, char *str2)
{
for (int i=0; str1[ i ] ; i++) str2 = str1[i];
str2[i] = '\0';
}


Edited by - stefu on February 11, 2002 12:38:47 AM

Share this post


Link to post
Share on other sites
or

  
void strcopy(char *str1, char *str2)
{
for (;*str1;)
*(str2++) = *(str1++);
*str2 = ''\0'';
}


or more clean
  
void strcopy(char *str1, char *str2)
{
for (;*str1; str1++, str2++)
*(str2) = *(str1);
*str2 = ''\0'';
}


warning the above code is considered advanced use of pointers and is BAD. dont use it, since it plays on tricks with postfix prefix incrments or in the second one with the for() fields. this last one is the worst. and should NEVER EVER be used no matter how advanced you are (though in fun code, its ok, not code that is suppsoed to do stuff).
btw i changed the varible names, cause the ones you used suck. use descriptive names. also dest comes first in most functions so its best to get in the habit of doing things like that.
  
void strcopy(char *dest, char *src)
{
while(*(dest++)=*(src++));
}
// or

void strcopy(char *dest, char *src)
{
for(;*(dest++)=*(src++););
}
// spiffy eh?



just goes to show the many ways to solve a problem (and this is not all the methdos that you can use).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yet another way:

void strcopy(char* str1,char* str2)
{
while (*str1++ = *str2++)
;
}

Much faster than the previous methods.

Share this post


Link to post
Share on other sites
  Just assign the length of the string before the loop and use the value in the condition.

str_length = strlen(str1);
for(i=0;i<str_length;i++)
{

}

Faster code


Yeah, I like your way better hehe, good to teach good
optomization techniques early.

Demonlir, if you're reading this, look at his code, it
takes up a little more memory, but instead of calling that
function every time of the loop, it checks it against the
variable, which is much faster. The computer knows that
the variable is for example 80. And instead of checking to see
how long the string is every time in the loop, it just checks it
against 80. Much more efficient.

-Lohrno

PS - heh I write that not only for him but for me! I gotta get
into those good habits too =)


Edited by - Lohrno on February 12, 2002 12:24:21 PM

Share this post


Link to post
Share on other sites

  • Advertisement