conflicting types or implicit declaration or incompatible implicitdeclaration

D

dentzigui

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?
 
S

Seebs

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
 
D

dentzigui

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

Ian Collins

dentzigui said:
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..).
 
S

Seebs

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
 
B

Ben Bacarisse

dentzigui said:
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.
 
K

Keith Thompson

Ben Bacarisse said:
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.
 
B

Ben Bacarisse

Keith Thompson said:
I think you mean "-std=c89" for the first one.

Rats! Yes, I did, thank you.
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.
 
S

Seebs

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
 

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,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top