Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > strcpy (s, "ABC);

Reply
Thread Tools

strcpy (s, "ABC);

 
 
gdotone@gmail.com
Guest
Posts: n/a
 
      07-25-2012
this code sample is taken from "A Book on C"

char s[100];

strcpy (s, "ABC");

....

the compiler issues a warning. Implicitly declaring C library function 'strcpy' with type 'char*(char *,const char *)'

Why does the compiler issue this warning, what does it mean, and what is the fix? compiler is LLVM 3.1



 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      07-25-2012
Paavo Helde <> wrote:
> However, this is C, not C++. As you have posted in a C++ newsgroup, here
> is the same code in C++:
>
> #include <string>
> int main() {
> std::string s = "ABC";
> }


Actually it isn't "the same code" because it uses dynamic memory allocation
while the C version didn't.

OTOH, it's a much *safer* version for sure.
 
Reply With Quote
 
 
 
 
Zoltan Juhasz
Guest
Posts: n/a
 
      07-25-2012
On Tuesday, 24 July 2012 20:12:07 UTC-4, gdo...@gmail.com wrote:
> this code sample is taken from &quot;A Book on C&quot;
>
> char s[100];
>
> strcpy (s, &quot;ABC&quot;
>
> ...
>
> the compiler issues a warning. Implicitly declaring C library function 'strcpy' with type 'char*(char *,const char *)'
>
> Why does the compiler issue this warning, what does it mean, and what is the fix? compiler is LLVM 3.1



I guess you were using Clang as the C/C++ front-end. In C
one can declare a function implicitly, by invoking the
function. I presume that Clang is aware of the standard
function 'strcpy', and issued a warning that you implicitly
declared a C library function.

Solution: have the 'string.h' header included in your
relevant source code file(s).


PS: this is a very common problem, you should have used
Google first.

-- Zoltan
 
Reply With Quote
 
Varun Tewari
Guest
Posts: n/a
 
      07-25-2012
U missing header file to find the forward declaration of strcpy.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-25-2012
On Wed, 2012-07-25, Juha Nieminen wrote:
> Paavo Helde <> wrote:
>> However, this is C, not C++. As you have posted in a C++ newsgroup, here
>> is the same code in C++:
>>
>> #include <string>
>> int main() {
>> std::string s = "ABC";
>> }

>
> Actually it isn't "the same code" because it uses dynamic memory allocation
> while the C version didn't.


[Disregarding that Paavo probably let "the same code" mean "the normal
way to do the corresponding thing in C++"]

I don't quite see why the dynamic allocation difference is noteworthy
here. Both languages support it, and there's nothing wrong with using
it.

> OTOH, it's a much *safer* version for sure.


Yes, and *that's* the difference. Not in this meaningless example of
course, but if you plan to use that string for something non-trivial.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      07-26-2012
Jorgen Grahn <grahn+> wrote:
> I don't quite see why the dynamic allocation difference is noteworthy
> here. Both languages support it, and there's nothing wrong with using
> it.


There are many situations where the speed difference is significant.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      07-26-2012
On Thu, 2012-07-26, Juha Nieminen wrote:
> Jorgen Grahn <grahn+> wrote:
>> I don't quite see why the dynamic allocation difference is noteworthy
>> here. Both languages support it, and there's nothing wrong with using
>> it.

>
> There are many situations where the speed difference is significant.


Yes, sure, but they are not so common that you go for char[N] by
default. (Also we shouldn't scare away C newbies by immediately
warning them about the inefficiencies of std::string.)

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strcpy of a class object leonkatz@gmail.com C++ 11 01-20-2005 04:44 AM
strcpy and the copy constructor RonHiler C++ 8 10-19-2004 06:30 AM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 4 09-14-2004 08:38 PM
C++ Compiler with a -Wwarn-use-of-strcpy or similar option?? Paul Sheer C++ 7 09-10-2004 05:07 PM
strcpy Mike Mimic C++ 9 05-17-2004 08:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57