conflicting types or implicit declaration or incompatible implicitdeclaration

Discussion in 'C Programming' started by dentzigui, Oct 31, 2009.

  1. dentzigui

    dentzigui Guest

    hi, i have being trying study k&r book. and i am puzzled with this:
    #include <stdio.h>

    char *strdup(char *);

    int main(void)
    {
    char *word = "this is a test";
    char *p;

    p = strdup(word);
    printf("%s\n", p);
    return 1;
    }

    char *strdup(char *s)
    {
    char *p;

    p = (char *) malloc(strlen(s) + 1);
    if (p != NULL)
    strcpy(p, s);
    return p;
    }

    when compiled i have:
    teste.c: In function ‘strdup’:
    teste.c:19: warning: implicit declaration of function ‘malloc’
    teste.c:19: warning: incompatible implicit declaration of built-in
    function ‘malloc’
    teste.c:19: warning: implicit declaration of function ‘strlen’
    teste.c:19: warning: incompatible implicit declaration of built-in
    function ‘strlen’
    teste.c:21: warning: implicit declaration of function ‘strcpy’
    teste.c:21: warning: incompatible implicit declaration of built-in
    function ‘strcpy’

    and when the same code but with:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    .....
    i have:
    teste.c:5: error: conflicting types for ‘strdup’
    teste.c:18: error: conflicting types for ‘strdup’

    could someone help me understand/learn?
     
    dentzigui, Oct 31, 2009
    #1
    1. Advertising

  2. dentzigui

    Seebs Guest

    Re: conflicting types or implicit declaration or incompatible implicit declaration

    On 2009-10-31, dentzigui <> wrote:
    > hi, i have being trying study k&r book. and i am puzzled with this:


    Hi!

    > teste.c: In function ?strdup?:
    > teste.c:19: warning: implicit declaration of function ?malloc?


    You've used a function called malloc which you hadn't declared, so it
    got an "implicit" declaration.

    > teste.c:19: warning: incompatible implicit declaration of built-in
    > function ?malloc?


    .... but it happens that this compiler already knows about malloc and
    warns you that the implicit declaration doesn't match.

    > teste.c:19: warning: implicit declaration of function ?strlen?
    > teste.c:19: warning: incompatible implicit declaration of built-in
    > function ?strlen?


    Ditto, for strlen.

    > teste.c:21: warning: implicit declaration of function ?strcpy?
    > teste.c:21: warning: incompatible implicit declaration of built-in
    > function ?strcpy?


    Ditto, for strcpy.

    > teste.c:5: error: conflicting types for ?strdup?
    > teste.c:18: error: conflicting types for ?strdup?


    > could someone help me understand/learn?


    In this case, you're being bitten by a common extension. The standard
    reserves all names starting with "str" followed immediately by a lowercase
    letter for possible extensions. Many systems already have a strdup()
    function with exactly this functionality, but a slightly different
    calling sequence. (On my system, "strdup" takes a "const char *"
    argument.)

    Try renaming the test function (and your calls to it) to something
    like my_strdup().

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Oct 31, 2009
    #2
    1. Advertising

  3. dentzigui

    dentzigui Guest

    Re: conflicting types or implicit declaration or incompatibleimplicit declaration

    On 31 out, 18:37, Seebs <> wrote:
    > On 2009-10-31, dentzigui <> wrote:
    >
    > > hi, i have being trying study k&r book. and i am puzzled with this:

    oops! i meant "...been..."


    >
    > In this case, you're being bitten by a common extension. The standard
    > reserves all names starting with "str" followed immediately by a lowercase
    > letter for possible extensions. Many systems already have a strdup()
    > function with exactly this functionality, but a slightly different
    > calling sequence. (On my system, "strdup" takes a "const char *"
    > argument.)
    >
    > Try renaming the test function (and your calls to it) to something
    > like my_strdup().


    thank's.

    hmmm... i know this is completly offtopic but for someone to become a
    c programmer i mean like a job and i dont mean like one of the most
    expertise just a simple job with c, how much does he should know? like
    after k&r and then what? and also in this computer-driven world what
    does
    **c programmers** program so much? and again i dont mean the ones most
    expertise, somehow (intuitively) i know that is "too much wild".
    errr... offtopic... i know...
     
    dentzigui, Oct 31, 2009
    #3
  4. dentzigui

    Ian Collins Guest

    Re: conflicting types or implicit declaration or incompatibleimplicit declaration

    dentzigui wrote:
    >
    > hmmm... i know this is completly offtopic but for someone to become a
    > c programmer i mean like a job and i dont mean like one of the most
    > expertise just a simple job with c, how much does he should know? like
    > after k&r and then what? and also in this computer-driven world what
    > does
    > **c programmers** program so much? and again i dont mean the ones most
    > expertise, somehow (intuitively) i know that is "too much wild".
    > errr... offtopic... i know...


    That all depends what and at what level the programmer is employed to
    do. As a novice (graduate) you would be expected to have a good grasp
    of the basics (K&R) as well as a reasonable grasp of the software
    development process.

    The more senior you get, the more domain specific knowledge you will
    require (for example embedded systems, networking, kernel etc..).

    --
    Ian Collins
     
    Ian Collins, Oct 31, 2009
    #4
  5. dentzigui

    Seebs Guest

    Re: conflicting types or implicit declaration or incompatible implicit declaration

    On 2009-10-31, dentzigui <> wrote:
    > hmmm... i know this is completly offtopic but for someone to become a
    > c programmer i mean like a job and i dont mean like one of the most
    > expertise just a simple job with c, how much does he should know?


    Depending on your market, I might look at English too -- you're mostly
    comprehensible, but spelling errors and missing punctuation can turn people
    off on a prospective hire.

    >like after k&r and then what?


    Depends a lot on what your interests are. These days, I'd say you should know
    at least one or two scripting languages and one or two lower-level languages.
    I quite like C, and I'm pretty fond of sh and Ruby as well. Of the C-like
    OO-ish languages, I like Objective-C and Java, but not so much C++, but I
    guess it's pretty market-specific.

    >and also in this computer-driven world what
    > does
    > **c programmers** program so much? and again i dont mean the ones most
    > expertise, somehow (intuitively) i know that is "too much wild".
    > errr... offtopic... i know...


    FWIW, the main stuff I deal with is actually C compilers, and those are in
    turn used for various applications and kernels for embedded systems.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Oct 31, 2009
    #5
  6. Re: conflicting types or implicit declaration or incompatible implicit declaration

    dentzigui <> writes:
    <snip>
    > and when the same code but with:
    > #include <stdio.h>
    > #include <string.h>
    > #include <stdlib.h>
    > ....
    > i have:
    > teste.c:5: error: conflicting types for ‘strdup’
    > teste.c:18: error: conflicting types for ‘strdup’
    >
    > could someone help me understand/learn?


    Rather than rename you function you can ask gcc to conform more
    closely to the standard. As a minimum, you need to say what version
    of C you want:

    gcc -std=c98 ...
    or
    gcc -std=c99 ...

    and if you add -pedantic, gcc you get even closer conformance to
    standard C.

    --
    Ben.
     
    Ben Bacarisse, Oct 31, 2009
    #6
  7. Re: conflicting types or implicit declaration or incompatible implicit declaration

    Ben Bacarisse <> writes:
    > dentzigui <> writes:
    > <snip>
    >> and when the same code but with:
    >> #include <stdio.h>
    >> #include <string.h>
    >> #include <stdlib.h>
    >> ....
    >> i have:
    >> teste.c:5: error: conflicting types for ‘strdup’
    >> teste.c:18: error: conflicting types for ‘strdup’
    >>
    >> could someone help me understand/learn?

    >
    > Rather than rename you function you can ask gcc to conform more
    > closely to the standard. As a minimum, you need to say what version
    > of C you want:
    >
    > gcc -std=c98 ...
    > or
    > gcc -std=c99 ...


    I think you mean "-std=c89" for the first one.

    > and if you add -pedantic, gcc you get even closer conformance to
    > standard C.


    Right, but strdup() is still in a reserved namespace. You can
    sometimes get away with declaring your own function by that name,
    but the compiler isn't required to let you.

    Call it "str_dup" or "dupstr" or something like that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 1, 2009
    #7
  8. Re: conflicting types or implicit declaration or incompatible implicit declaration

    Keith Thompson <> writes:

    > Ben Bacarisse <> writes:
    >> dentzigui <> writes:
    >> <snip>
    >>> and when the same code but with:
    >>> #include <stdio.h>
    >>> #include <string.h>
    >>> #include <stdlib.h>
    >>> ....
    >>> i have:
    >>> teste.c:5: error: conflicting types for ‘strdup’
    >>> teste.c:18: error: conflicting types for ‘strdup’
    >>>
    >>> could someone help me understand/learn?

    >>
    >> Rather than rename you function you can ask gcc to conform more
    >> closely to the standard. As a minimum, you need to say what version
    >> of C you want:
    >>
    >> gcc -std=c98 ...
    >> or
    >> gcc -std=c99 ...

    >
    > I think you mean "-std=c89" for the first one.


    Rats! Yes, I did, thank you.

    >> and if you add -pedantic, gcc you get even closer conformance to
    >> standard C.

    >
    > Right, but strdup() is still in a reserved namespace. You can
    > sometimes get away with declaring your own function by that name,
    > but the compiler isn't required to let you.
    >
    > Call it "str_dup" or "dupstr" or something like that.


    That's good advice, but since the error was not the OP's by K&R's I
    thought simply posting how to get rid of the compiler warning so the
    examples would compile "clean" was worth while.

    Do the examples in K&R2 also invade this namespace? I know they do in
    the first edition but I've never bought the second.

    --
    Ben.
     
    Ben Bacarisse, Nov 1, 2009
    #8
  9. dentzigui

    Seebs Guest

    Re: conflicting types or implicit declaration or incompatible implicit declaration

    On 2009-11-01, Ben Bacarisse <> wrote:
    > Do the examples in K&R2 also invade this namespace? I know they do in
    > the first edition but I've never bought the second.


    Yes. They provide sample implementations of a couple of str*() functions
    and also qsort(). It was a heady and lawless time.

    -s
    --
    Copyright 2009, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
     
    Seebs, Nov 1, 2009
    #9
    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. Replies:
    2
    Views:
    4,647
  2. Paminu
    Replies:
    7
    Views:
    2,396
    FlyingBird
    Feb 7, 2006
  3. Replies:
    3
    Views:
    818
  4. Michael.Z
    Replies:
    11
    Views:
    819
    Michael.Zhang
    May 11, 2008
  5. Michael Doubez
    Replies:
    11
    Views:
    961
    James Kanze
    Mar 26, 2011
Loading...

Share This Page