strcpy (s, "ABC);

G

gdotone

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
 
J

Juha Nieminen

Paavo Helde said:
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.
 
Z

Zoltan Juhasz

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
 
J

Jorgen Grahn

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
 
J

Juha Nieminen

Jorgen Grahn said:
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.
 
J

Jorgen Grahn

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top