0
void StrCpy(char * Dest , char* Src )
{
 
  while(*Src){
    *Dest = *Src++;
    cout << *Dest ++;    
    
  }
}

void main()
{  

char* string1 = "\0";
char * string2 = "EL";

StrCpy(string1, string2);
//cout << string1; //must output EL
}

Im trying to output EL after its copied to string1 but nothing its printed

7
Contributors
18
Replies
19
Views
8 Years
Discussion Span
Last Post by namehere05
Featured Replies
  • [QUOTE=StuXYZ;748756]can I just add, you need to remember to add a [icode]*dest=0;[/icode] line after the while. You will need something similar in any of the other posts.[/QUOTE] No with Arkm's algorithm you don't because it will also copy the null terminator. Read More

  • 1
    Agni 370   8 Years Ago

    [code] for(i = 0 ; *Src!='\0' ; Src++) tmp[i]=*Src; [/code] should increment 'i' also. as of now 'i' remains 0 throughout. Read More

0

You are attempting to stuff 3 bytes (string2) into a character array that is only 1 byte (string1). Try this:

int main()
{  

char string1[4] = {0}
char * string2 = "EL";

StrCpy(string1, string2);
//cout << string1; //must output EL
}
0

it works!! .. and I heard of that before but got confused cuz the thing I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either so I thought that wasnt the problem as I thought there was a solution without need to "mix" char[] and char *
Could you tell me why using a longer string than string2 to initialize char* pointer wouldnt work in my method
I thought I would get something like this:

char* string 1 = "longer than string2"
char* string 2 = "NONSENSE"

func(string1 string2)//same as before
cout << string1 ; //I was expecting it to print changed string1 as
"NONSENSEhan string2"
in other words overwritting

0

>>I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either
I didn't work because you can't change string literals, which is what you attempted to do. But you could have done it like this: char string1[] = "longer than string2" , which will put the text in writeable memory.

0

>>I did was to Initialize char * string1 = "longer than string2" and evrything else the same and it didnt work either
I didn't work because you can't change string literals, which is what you attempted to do. But you could have done it like this: char string1[] = "longer than string2" , which will put the text in writeable memory.

I thought you could always assign a char* to another char*. Are you sure that declaring

char* string1 = "Bigger than String2";

wont work?

0

well thank you all for answering.
"and in other news".. I know .. "Have the feeling" its been answered tons of time but kinda not my fault;
I cheked many introduction texts about that and they dont say how they (the [] and the char * or the string class declarations) happend to be related.
they teach either one and/or the other as independent but not how they in this case "mix"; the do's and dont's are either one declartion or another weighted

0

Remember the K&R masterpiece:

while (*dest++ = *src++);

Unforgettable Algol 68's genes are still alive in C and C++ ;)...

0

Remember the K&R masterpiece:

while (*dest++ = *src++);

Unforgettable Algol 68's genes are still alive in C and C++ ;)...

can I just add, you need to remember to add a *dest=0; line after the while. You will need something similar in any of the other posts.

1

can I just add, you need to remember to add a *dest=0; line after the while. You will need something similar in any of the other posts.

No with Arkm's algorithm you don't because it will also copy the null terminator.

Votes + Comments
Your correct, I don't know why I hadn't noticed that!
0

Regrettably, it's not ArkM's algorithm. It's K&R code.
Thank you, Ancient Dragon: this code is OK, it copies null byte ;)

0

What about Buffer Overflow? :P

Look at the requirements: if you forgot strcpy specification I can recall, try to search the target size there ;)

0

What about Buffer Overflow? :P

That is a problem with all standard C functions. If you use one of the new Microsoft compilers, Microsoft declared them depreciated and wants us to use the _s functions, such as strcpy_s(), which has an additional destination buffer size parameter. But even that could cause buffer overflow if the calling function passed the wrong buffer size. There is no 100% bullet-proof method in C to prevent buffer overflow. But if you follow the rules and bother to actually read the function's requirements then you can minimize the buffer overflow problem.

0

I modified the program it looks like this but no doubt theres something wrong as it wont output anything I await your wisdom guys

#include <iostream>
#include <cstring>
using std::cin;
using std::cout;

void StrCpy(char * Dest , char* Src )
{
 int i;
 char *tmp = new char[strlen(Src)+1];
 for(i = 0 ; *Src!='\0' ; Src++)
    tmp[i]=*Src;
 tmp[i]='\0'; 
 Dest = tmp; 
 for(int i= 0 ; tmp[i]!='\0' ; i++)
   cout << tmp[i];
}

void main()
{  

char *string1;
char * string2 = "EL";


StrCpy(string1, string2);
 // while(*string1)
 //   cout << *string1++; 
}
0

Your version doesn't work because the first parameter Dest needs to be passed by pointer, not by value

#include <iostream>
#include <cstring>
using std::cin;
using std::cout;

void StrCpy(char ** Dest , char* Src )
{
 int i;
 char *tmp = new char[strlen(Src)+1];
 for(i = 0 ; *Src!='\0' ; Src++)
    tmp[i]=*Src;
 tmp[i]='\0'; 
 *Dest = tmp; 
 for(int i= 0 ; tmp[i]!='\0' ; i++)
   cout << tmp[i];
}

void main()
{  

char *string1 = 0;
char * string2 = "EL";


StrCpy( &string1, string2);
 // while(*string1)
 //   cout << *string1++; 
}
1
for(i = 0 ; *Src!='\0' ; Src++)
    tmp[i]=*Src;

should increment 'i' also. as of now 'i' remains 0 throughout.

Votes + Comments
Good catch :)
0

It worked wonders, just a lil bit confused why the extra pointer indirection was needed but I think that'll come in the future

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.