• Create Account

Why Doesn't this Work?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

#1Demonlir  Members

Posted 11 February 2002 - 02:26 PM

#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'';
}

#2Lohrno  Members

Posted 11 February 2002 - 03:06 PM

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

#3Gaiiden  Senior Staff

Posted 11 February 2002 - 03:20 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??

#4clabinsky  Members

Posted 11 February 2002 - 03:31 PM

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
. 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

Faster code

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

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

#5stefu  Members

Posted 11 February 2002 - 05:38 PM

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

#6a person  Members

Posted 11 February 2002 - 05:49 PM

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).

#7Anonymous Poster_Anonymous Poster_*  Guests

Posted 11 February 2002 - 05:50 PM

Yet another way:

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

Much faster than the previous methods.

#8Dave_Hunt  Members

Posted 11 February 2002 - 05:52 PM

BTW, that was my anonymous post above. Of course, it doesn''t matter since "a person" beat me to it

#9Lohrno  Members

Posted 12 February 2002 - 05:23 AM

 ` 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

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.