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. Advertisements

  2. dentzigui

    Seebs Guest

    You've used a function called malloc which you hadn't declared, so it
    got an "implicit" declaration.
    .... but it happens that this compiler already knows about malloc and
    warns you that the implicit declaration doesn't match.
    Ditto, for strlen.
    Ditto, for strcpy.
    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
     
    Seebs, Oct 31, 2009
    #2
    1. Advertisements

  3. dentzigui

    dentzigui Guest

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

    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

    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, Oct 31, 2009
    #4
  5. dentzigui

    Seebs Guest

    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.
    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.
    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
     
    Seebs, Oct 31, 2009
    #5
  6. 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 Bacarisse, Oct 31, 2009
    #6
  7. I think you mean "-std=c89" for the first one.
    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, Nov 1, 2009
    #7
  8. Rats! Yes, I did, thank you.
    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 Bacarisse, Nov 1, 2009
    #8
  9. dentzigui

    Seebs Guest

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

    -s
     
    Seebs, Nov 1, 2009
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.