473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

strcpy(); function trouble

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:

Nov 17 '05 #1
3 2644
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/
Nov 17 '05 #2
nirvana4lf wrote:
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
Nov 17 '05 #3
Carl Daniel [VC++ MVP] wrote:
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.
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
9
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1)...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
9
by: jim | last post by:
i want to make a c file that i can 'scanf ' students scores of 2 classes and their names , and i want it to get the sum of the 2 scores and make them in order .at last 'printf' /*am sorry,my...
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
4
by: chikito.chikito | last post by:
1. Can someone tell me the difference between these two functions: void strcpy(char *s1, const char *s2) { while(*s1++ = *s2++) ; } //function prototype of strcpy follows char...
77
by: arnuld | last post by:
I have created my own implementation of strcpy library function. I would like to have comments for improvements: /* My version of "strcpy - a C Library Function */ #include <stdio.h>...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.