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

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
}

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

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

And in other news, reports are coming in from all over the web about the same thing happening elsewhere.

Experts agree that there is only one possible solution.

OK.. my bad... True .. it will not work.

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

Remember the K&R masterpiece:

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

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

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.

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.

commented: Your correct, I don't know why I hadn't noticed that! +1

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

What about Buffer Overflow? :P

What about Buffer Overflow? :P

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

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.

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++; 
}

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++; 
}
for(i = 0 ; *Src!='\0' ; Src++)
    tmp[i]=*Src;

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

commented: Good catch :) +36

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.