473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

strcpy warning

Hi. During my program's set-up phase where it reads in the arguments it
was invoked with, I programmed this:
if (strcmp(argv[i],"-G") ==0)
{
geom_scaling = ON;
if (i < argc-1)
strcpy(geom_string,argv[i+1]);
}
(
i is a loop counter,
geom_scaling has been declared as int,
ON has been #defined,
geom_string has been declared thus: char geom_string[64];
--the program is for my own use only, and a length of 64 is way way longer
than could ever make sense, hence the lack of error checking.
)

Could someone tell me whether to worry about the following compiler
warning, and how to fix it if needs be:

warning: type mismatch in implicit declaration for built-in function
`strcpy'

As you will have guessed, I'm a beginner. Thanks --Ape
Nov 14 '05 #1
9 7726
Ape Ricket wrote:
warning: type mismatch in implicit declaration for built-in function
`strcpy'


You are probably not including string.h.
Fix by adding
#include <string.h>
somewhere near the top of the file. (not necessary to put at top,
but usually that is what you do)

For a more in depth discussion on the issue read up on function
prototypes.

--
Thomas.

Nov 14 '05 #2
Ape Ricket <ar@example.com> writes:
warning: type mismatch in implicit declaration for built-in function
`strcpy'


Add #include <string.h> at the top of your program.
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
Nov 14 '05 #3
On 2 Jan 2004 00:06:28 GMT, Ape Ricket <ar@example.com> wrote:
Hi. During my program's set-up phase where it reads in the arguments it
was invoked with, I programmed this:
if (strcmp(argv[i],"-G") ==0)
{
geom_scaling = ON;
if (i < argc-1)
Consistent indenting will make your programs much easier to read.
strcpy(geom_string,argv[i+1]);
}
(
i is a loop counter,
geom_scaling has been declared as int,
ON has been #defined,
geom_string has been declared thus: char geom_string[64];
--the program is for my own use only, and a length of 64 is way way longer
than could ever make sense, hence the lack of error checking.
)

Could someone tell me whether to worry about the following compiler
warning, and how to fix it if needs be:

warning: type mismatch in implicit declaration for built-in function
`strcpy'
Did you #include string.h? Can you cut and paste a complete
compilable short program that demonstrates the problem.

As you will have guessed, I'm a beginner. Thanks --Ape


<<Remove the del for email>>
Nov 14 '05 #4
Barry Schwarz <sc******@deloz.net> wrote:

if (strcmp(argv[i],"-G") ==0)
{
geom_scaling = ON;
if (i < argc-1)


Consistent indenting will make your programs much easier to read.
strcpy(geom_string,argv[i+1]);
}
hmmm, it looks to me as though each successive indentation has 4 extra
spaces. What's wrong with it?

Could someone tell me whether to worry about the following compiler
warning, and how to fix it if needs be:

warning: type mismatch in implicit declaration for built-in function
`strcpy'


Did you #include string.h?


Thanks Barry and others, I hadn't #included string.h, and doing so made
the warning go away. I find it odd that the program worked as
expected without the header file--where did gcc get the strcmp, strcpy and
strtok functions from, if I didn't #include the string library?

Is there a default version of strcpy that I was misusing, but not badly
enough to cause an error--just a compiler warning?

Another funny thing is that when I log in to the account where I do my
work, I sometimes get dropped into a server running UNIX, and sometimes
linux. The warnings on the version of gcc running on each are
different--linux doesn't care as much about the missing header file. And
neither compiler minded about strcmp, but strtok and strcpy both
engendered warnings. (As you can imagine this is a pain in the ass--if I
compile something on one architecture it won't run next time I log on and
get put on the other architecture. But this is OT and I apologise.)

Ape
Nov 14 '05 #5

On Fri, 2 Jan 2004, Ape Ricket wrote:

Barry Schwarz <sc******@deloz.net> wrote:

if (strcmp(argv[i],"-G") ==0)
{
geom_scaling = ON;
if (i < argc-1)
Consistent indenting will make your programs much easier to read.
strcpy(geom_string,argv[i+1]);
}
hmmm, it looks to me as though each successive indentation has 4 extra
spaces. What's wrong with it?


For some reason, I used to indent programs like this, back in my
Turbo C days:

if (foo)
{
int bar;
baz();
}
else
quux();

and I guess I must have found it readable at the time. But I now
find it a really ugly and illegible style, and don't remember how I
could possibly have found it logical. Your style is similarly
unidiomatic and thus hard-to-read for most of the regulars here
(including myself). I recommend switching. ;-)

if (foo) {
int bar;
baz();
}
else
quux();

is my preferred style now, for example. Note the key changes: braces
line up with their controlling statements, or are on the same line;
declarations line up with the statements that use them. [I also
make a rule of 2-space indentation for the bodies of control statements
without enclosing braces, like 'quux' above -- but that's not a
widespread usage, AFAIK.]

warning: type mismatch in implicit declaration for built-in function
`strcpy'


Did you #include string.h?


Thanks Barry and others, I hadn't #included string.h, and doing so made
the warning go away. I find it odd that the program worked as
expected without the header file--where did gcc get the strcmp, strcpy and
strtok functions from, if I didn't #include the string library?


Most compilers include the standard "libc" *library* whether or not
you #include the standard headers. So the linker knows where to find
these functions; it just doesn't know what type of parameters they
take, or what types they return. On your system, it turns out not to
matter to the machine code what the types are. This isn't true in
general, though. And gcc usually *won't* include the math "libm"
library unless you not only #include <math.h>, but *also* pass the
-lm flag to the 'gcc' program! Read the FAQ.
Is there a default version of strcpy that I was misusing, but not badly
enough to cause an error--just a compiler warning?
Sort of. Some compilers *would* give you an error; some linkers
would give you a linker error; but not gcc. See gnu.gcc.help for
more information on GCC in particular.
Another funny thing is that when I log in to the account where I do my
work, I sometimes get dropped into a server running UNIX, and sometimes
linux. The warnings on the version of gcc running on each are
different--linux doesn't care as much about the missing header file. And
neither compiler minded about strcmp, but strtok and strcpy both
engendered warnings.


strcmp() returns 'int', which is the default. 'strtok' and 'strcpy'
both return 'char *', which is not the default. C89 has undeclared
functions return 'int' by default, as if you had declared them

int strcmp();
int strtok();
int strcpy();

This is still wrong, of course, but not wrong enough in strcmp()'s
case to give you a warning about it.

HTH,
-Arthur
Nov 14 '05 #6
Ape Ricket wrote:
Barry Schwarz <sc******@deloz.net> wrote:
if (strcmp(argv[i],"-G") ==0)
{
geom_scaling = ON;
if (i < argc-1)
Consistent indenting will make your programs much easier to read.

strcpy(geom_string,argv[i+1]);
}

hmmm, it looks to me as though each successive indentation has 4 extra
spaces. What's wrong with it?


The 'if' is indented relative to the geom_scaling assignment, even
though they are at the same "level".

Could someone tell me whether to worry about the following compiler
warning, and how to fix it if needs be:

warning: type mismatch in implicit declaration for built-in function
`strcpy'
Did you #include string.h?

Thanks Barry and others, I hadn't #included string.h, and doing so made
the warning go away. I find it odd that the program worked as
expected without the header file--where did gcc get the strcmp, strcpy and
strtok functions from, if I didn't #include the string library?


You don't #include libraries. You #include headers. Headers are very
different from libraries. Headers generally contain declarations - the
actual implementations are elsewhere. It's up to the linker to locate
and link against the correct libraries. For most compilers, there is a
set of default libraries it will check, and strcmp etc. are probably in
that default set.

The reason it sort of works without the #include is that versions of C
prior to C99 allowed implicit declaration of a function just by calling
it. This is usually a Bad Thing, since the implicit declaration may not
match the actual form of the function, and it causes the old calling
convention to be used (where arguments are promoted and no type checking
is done).

Is there a default version of strcpy that I was misusing, but not badly
enough to cause an error--just a compiler warning?
Most likely you were using the "real" strcpy. The incorrect (implicit)
declaration of it would make the behavior undefined. Appearing to work
correctly is one of the infinite possibilities that may result from
undefined behavior.

Another funny thing is that when I log in to the account where I do my
work, I sometimes get dropped into a server running UNIX, and sometimes
linux. The warnings on the version of gcc running on each are
different--linux doesn't care as much about the missing header file. And
neither compiler minded about strcmp, but strtok and strcpy both
engendered warnings.
It was probably smart enough to realize that the implicit declaration of
strcmp was correct (because it happens to return 'int', which is what
the return type defaults to in an implicit declaration). As for the
difference in diagnostics, it could be different versions of the
compiler or it could be using different default settings. I think gcc
has some config files that control default options.
(As you can imagine this is a pain in the ass--if I
compile something on one architecture it won't run next time I log on and
get put on the other architecture. But this is OT and I apologise.)


If that's the case you need to 1) write your code more portably and 2)
enable a more strict level of warnings and standard-compliance. Here are
the options I use for gcc. If you want, you can use this as a starting
point and find a good set of options for you.

-W -Wall -Wcast-align -Wwrite-strings -Wno-sign-compare
-Wno-unused-parameter -Wpointer-arith -Wundef -Wcast-qual
-Wunreachable-code -ansi -pedantic-errors

I just added the last 3 -W options, so I don't really know how they'll
work out yet. I might decide I don't like them.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #7
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
[...]
if (foo) {
int bar;
baz();
}
else
quux();

is my preferred style now, for example. Note the key changes: braces
line up with their controlling statements, or are on the same line;
declarations line up with the statements that use them. [I also
make a rule of 2-space indentation for the bodies of control statements
without enclosing braces, like 'quux' above -- but that's not a
widespread usage, AFAIK.]


I *always* use braces on control statements, except in rare cases
where the whole thing is short enough to fit on one line. It's a
habit I picked up from Perl, which requires the braces, but I find it
useful; it looks more consistent, and makes maintenance easier when I
decide later that I want more than one statement.

The above would be:

if (foo) {
int bar;
baz();
}
else {
quux();
}

I'm not arguing that everyone must do this, it's just the style I like
to use.

In rare cases I might compress the layout to something like this:

if (foo) do_foo_stuff();
elsif (bar) do_bar_stuff();
elsif (baz) do_baz_stuff();
elsif (quux) do_quux_stuff();
else do_something_else();

but I often regret it later when I'm maintaining the code (which I
spend a lot more time on than writing it in the first place).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #8

On Fri, 2 Jan 2004, Keith Thompson wrote:

"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
[...]
if (foo) {
int bar;
baz();
}
else
quux(); [I also make a rule of 2-space indentation for the bodies of
control statements without enclosing braces, like 'quux' above
-- but that's not a widespread usage, AFAIK.]
I *always* use braces on control statements, except in rare cases
where the whole thing is short enough to fit on one line. It's a
habit I picked up from Perl, which requires the braces, but I find it
useful [...]

else {
quux();
}

I'm not arguing that everyone must do this, it's just the style I like
to use.
I completely agree with you as far as the example I chose -- in a
real program, I would almost always put braces around the body of
an 'else' clause, especially in cases where the matching 'if' clause
had them. But I often write things like

int foo(void)
{
FILE *fp = fopen("bar", "r");

if (fp == NULL)
return -1;

...

In that case I find it more convenient *not* to add braces; and
if I'm not going to add braces, I find it more consistent *not* to
indent the body a full level. That way, whenever I'm maintaining
my own style of code, I can tell instantly if a closing brace is
missing: e.g., if I see

}
}

or
}
}

then I immediately *know* that there's a problem. Of course,
being able to rely on this "gimmick" in my own code means that I
have a harder time with other people's code than I might if I just
wrote awfully-formatted code to begin with. ;-)

In rare cases I might compress the layout to something like this:

if (foo) do_foo_stuff();
elsif (bar) do_bar_stuff(); ^^^^^
Assuming, of course, that you were programming in CPerl. :)
but I often regret it later when I'm maintaining the code (which I
spend a lot more time on than writing it in the first place).


Yes; I used to do this, but then realized that it was going to
take up a lot of space on the page no matter how I indented it,
so I might as well be consistent.

-Arthur

Nov 14 '05 #9
"Arthur J. O'Dwyer" <aj*@nospam.andrew.cmu.edu> writes:
On Fri, 2 Jan 2004, Keith Thompson wrote:

[...]
I *always* use braces on control statements, except in rare cases
where the whole thing is short enough to fit on one line. It's a
habit I picked up from Perl, which requires the braces, but I find it
useful [...]

else {
quux();
}

I'm not arguing that everyone must do this, it's just the style I like
to use.


I completely agree with you as far as the example I chose -- in a
real program, I would almost always put braces around the body of
an 'else' clause, especially in cases where the matching 'if' clause
had them. But I often write things like

int foo(void)
{
FILE *fp = fopen("bar", "r");

if (fp == NULL)
return -1;

...

In that case I find it more convenient *not* to add braces; and
if I'm not going to add braces, I find it more consistent *not* to
indent the body a full level.

[...]

MMV (My Mileage Varies); I still prefer to use the braces in that
case. DGNED (De Gustibus Non Est Disputandum).

[...]
In rare cases I might compress the layout to something like this:

if (foo) do_foo_stuff();
elsif (bar) do_bar_stuff();

^^^^^
Assuming, of course, that you were programming in CPerl. :)


DOH (Dumb Oversight Here)!

I could claim that I use "#define elsif else if", but that would make
me look even more stupid. I meant "else if", of course.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Sheer | last post by:
I need to automatically search and replace all fixed size buffer strcpy's with strncpy's (or better yet, strlcpy's) as a security and stability audit. The code base is large and it is not feasable...
1
by: Josh Wilson | last post by:
Hey gang, So I have my stdin which is user defined file, and am wanting to write it to a temporary file (seems odd I know, but there is a sincere reason), and then use that file as the stdin for...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
302
by: Lee | last post by:
Hi Whenever I use the gets() function, the gnu c compiler gives a warning that it is dangerous to use gets(). Is this due to the possibility of array overflow? Is it correct that the program...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
55
by: Jake Thompson | last post by:
I need to copy a value into a char * field. I am currently doing this strcpy(cm8link.type,"13"); but I get an error of error C2664: 'strcpy' : cannot convert parameter 1 from 'const char'...
9
by: jim | last post by:
i want to make a c file that i can 'scanf ' students scores of 2 classes and their names , and i want it to get the sum of the 2 scores and make them in order .at last 'printf' /*am sorry,my...
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
2
by: bitstreamer | last post by:
I think strcpy() operations especially in external functions is one of C's most underrated (in difficulty) thing, as you can easily destroy your complete memory structure by forgetting to allocate...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.