strcpy(); function trouble

  • Thread starter Thread starter nirvana4lf
  • Start date Start date
N

nirvana4lf

this is probably a noob question but wutever. im using the strcpy();
function but it isnt working. here is an example:

#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>


using namespace std;


int main()
{

string src = "cookie";
string dest = "cool";
string *strcpy(string dest,string src);
cout << dest << endl;

system("pause");

return 0;
}

but it does not connect the two strings!!! please help!!!! :shock:
:shock: :shock:
 
Hi nirvana4lf,
string src = "cookie";
string dest = "cool";
string *strcpy(string dest,string src);
cout << dest << endl;


but it does not connect the two strings!!! please help!!!! :shock:

You are *mixing* C and C++!!!
This can be done, but it is not very common...

What is the goal? 'dest = "coolcookie"'?


Solution in C++:
string src = "cookie";
string dest = "cool";
dest.append(src);


--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
 
nirvana4lf said:
this is probably a noob question but wutever. im using the strcpy();
function but it isnt working. here is an example:

#include <stdio.h>
#include <iostream>
#include <string>
#include <stdlib.h>


using namespace std;


int main()
{

string src = "cookie";
string dest = "cool";
string *strcpy(string dest,string src);

This is declaring a function named strcpy that takes two parameters of type
string and returns string*. Nothing here is attempting to call strcpy at
all.
cout << dest << endl;

system("pause");

return 0;
}

but it does not connect the two strings

See Jochen's response for a "C++" solution. If you really wanted a 'C'
solution, it might look something like this:

#include <stdio.h>
#include <string.h>

int main()
{
char buffer[100];
// note: potential buffer overrun if strings are too long!
strcpy(buffer,"cookie");
strcat(buffer,"cool");
printf("%s\n",buffer);
}

The easily encountered buffer overrun is one big reason to prefer the C++
solution which suffers no such characteristic.

-cd
 
Carl said:
See Jochen's response for a "C++" solution. If you really wanted a 'C'
solution, it might look something like this:

#include <stdio.h>
#include <string.h>

int main()
{
char buffer[100];
// note: potential buffer overrun if strings are too long!
strcpy(buffer,"cookie");
strcat(buffer,"cool");
printf("%s\n",buffer);
}

The easily encountered buffer overrun is one big reason to prefer the C++
solution which suffers no such characteristic.

-cd


Or you could stick to the safe versions and use strncat, strncpy, and
snprintf. Through they need to be wrapped to work properly: while they
guarantee to not overrun, some of those functions do not guarantee null
termination and that behavior needs patching in too.
 
Back
Top