Why Doesn't this Work?

Started by
7 comments, last by Demonlir 22 years, 2 months ago
#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 = str1; str2 = ''\0''; } </i>
Another poll revealed that "Religion is top priority for Americans". Forty percent "said they valued their relationship with God above all else"; 29 percent chose "good health" and 21 percent "happy marriage." Satisfying work was chosen by 5 percent, respect of people by 2 percent. That this world might offer basic features of a human existence is hardly to be contemplated. These are the kinds of results one might find in a shattered peasent society.Another poll revealed that "Religion is top priority for Americans". Forty percent "said they valued their relationship with God above all else"; 29 percent chose "good health" and 21 percent "happy marriage." Satisfying work was chosen by 5 percent, respect of people by 2 percent. That this world might offer basic features of a human existence is hardly to be contemplated. These are the kinds of results one might find in a shattered peasent society.-Noam Chomsky
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
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??


Drew Sikora
Executive Producer
GameDev.net

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
Why make it simple when you can make it sooo nice and complicated?
Another way:
void strcopy(char *str1, char *str2){    for (int i=0; str1 ; i++) str2<i> = str1;<br>    str2 = '\0';<br>}  </pre>     </i> <br><br>Edited by - stefu on February 11, 2002 12:38:47 AM    
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++));}// orvoid 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).
Yet another way:

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

Much faster than the previous methods.
BTW, that was my anonymous post above. Of course, it doesn''t matter since "a person" beat me to it
  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

This topic is closed to new replies.

Advertisement