strcpy (s, "ABC);

Discussion in 'C++' started by gdotone@gmail.com, Jul 25, 2012.

  1. Guest

    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
     
    , Jul 25, 2012
    #1
    1. Advertising

  2. Re: [OT] Re: strcpy (s, "ABC);

    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.
     
    Juha Nieminen, Jul 25, 2012
    #2
    1. Advertising

  3. On Tuesday, 24 July 2012 20:12:07 UTC-4, 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
     
    Zoltan Juhasz, Jul 25, 2012
    #3
  4. Varun Tewari Guest

    U missing header file to find the forward declaration of strcpy.
     
    Varun Tewari, Jul 25, 2012
    #4
  5. Jorgen Grahn Guest

    Re: [OT] Re: strcpy (s, "ABC);

    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 .
     
    Jorgen Grahn, Jul 25, 2012
    #5
  6. Re: [OT] Re: strcpy (s, "ABC);

    Jorgen 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.
     
    Juha Nieminen, Jul 26, 2012
    #6
  7. Jorgen Grahn Guest

    Re: [OT] Re: strcpy (s, "ABC);

    On Thu, 2012-07-26, Juha Nieminen wrote:
    > Jorgen 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 .
     
    Jorgen Grahn, Jul 26, 2012
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jiong Feng
    Replies:
    0
    Views:
    843
    Jiong Feng
    Nov 19, 2003
  2. Bruce Sam
    Replies:
    15
    Views:
    7,933
    John C. Bollinger
    Nov 19, 2004
  3. vsgdp

    ABC inheriting from ABC

    vsgdp, Sep 24, 2005, in forum: C++
    Replies:
    1
    Views:
    312
    vsgdp
    Sep 24, 2005
  4. Gunter Henriksen

    x.abc vs x['abc']

    Gunter Henriksen, May 13, 2009, in forum: Python
    Replies:
    1
    Views:
    357
    alex23
    May 15, 2009
  5. Replies:
    4
    Views:
    154
Loading...

Share This Page