* assignment makes pointer from integer without a cast?
@ 2004-12-13 0:52 Patrick
2004-12-13 1:47 ` Hossein Mobahi
0 siblings, 1 reply; 5+ messages in thread
From: Patrick @ 2004-12-13 0:52 UTC (permalink / raw)
To: linux-c-programming
Hello,
New to the list and to C. Read O'Reilly's Practical C Programming and
another C book and am now trying to evolve beyond helloworld.c. When I
compile the snippet below, gcc-3.4.2 on Fedora Core 3 spits out:
test2.c:11: warning: assignment makes pointer from integer without a
cast
test2.c:12: warning: passing arg 1 of `usage' from incompatible pointer
type
I just can't figure out why it does that and how to fix it since my
pointer/casting/C knowledge is definitely lacking. The snippet does work
ok. Anyone care to enlighten me?
#include <stdio.h>
void usage(int *prog_ptr)
{
fprintf(stdout,"progname is: %s\n",*prog_ptr);
}
int main(int argc, char *argv[])
{
int *prog_ptr;
prog_ptr = basename (argv[0]);
usage(&prog_ptr);
return(0);
}
TIA,
Patrick
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: assignment makes pointer from integer without a cast?
2004-12-13 0:52 assignment makes pointer from integer without a cast? Patrick
@ 2004-12-13 1:47 ` Hossein Mobahi
2004-12-13 16:13 ` Francesco Gadaleta
0 siblings, 1 reply; 5+ messages in thread
From: Hossein Mobahi @ 2004-12-13 1:47 UTC (permalink / raw)
To: linux-c-programming
Patrick
Your program has a few incorrect assignments. Is
prog_ptr going to hold a string? I guess so because
you want to print program name. So "int *prog_ptr" is
wrong, use "char *prog_ptr" instead, i.e. an address
pointing to the first character of a given string.
Moreover, you do not need to use ampersand to obtain
prog_ptr's address because it is already a pointer
itself not an ordinary variable.
Eventually, you must pass a pointer to fprintf when
you use %s. So the * is incorrect (*prog_ptr returns a
character only, the first character of your string,
which is not what you want).
I'd rewrite your program as follows:
#include <stdio.h>
void usage(char *prog_ptr) /* not int* */
{
fprintf(stdout,"progname is: %s\n",prog_ptr); /* not
*prog_ptr */
}
int main(int argc, char *argv[])
{
char *prog_ptr; /* not int * */
prog_ptr = basename (argv[0]);
usage(prog_ptr); /* not &prog_ptr */
return(0);
}
--- Patrick <patrickC@puzzled.xs4all.nl> wrote:
> #include <stdio.h>
>
> void usage(int *prog_ptr)
> {
> fprintf(stdout,"progname is: %s\n",*prog_ptr);
> }
>
> int main(int argc, char *argv[])
> {
> int *prog_ptr;
> prog_ptr = basename (argv[0]);
> usage(&prog_ptr);
> return(0);
> }
__________________________________
Do you Yahoo!?
Yahoo! Mail - Helps protect you from nasty viruses.
http://promotions.yahoo.com/new_mail
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: assignment makes pointer from integer without a cast?
2004-12-13 1:47 ` Hossein Mobahi
@ 2004-12-13 16:13 ` Francesco Gadaleta
2004-12-13 19:53 ` Jan-Benedict Glaw
0 siblings, 1 reply; 5+ messages in thread
From: Francesco Gadaleta @ 2004-12-13 16:13 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1184 bytes --]
> void usage(char *prog_ptr) /* not int* */
> {
> fprintf(stdout,"progname is: %s\n",prog_ptr); /* not
> *prog_ptr */
> }
>
> int main(int argc, char *argv[])
> {
> char *prog_ptr; /* not int * */
> prog_ptr = basename (argv[0]);
> usage(prog_ptr); /* not &prog_ptr */
> return(0);
> }
>
>
change this line:
prog_ptr = basename (argv[0]);
with this:
prog_ptr = (char*) basename (argv[0]);
--
_________________________________________________
Imagine no possessions
I wonder if you can
No need for greed or hunger
A brotherhood of man
Imagine all the people
Sharing all the world...
John Lennon
_________________________________________________
_________________________________________________
Francesco Gadaleta - Italian GNU/Linux User
web site: www.gadaleta.org
email : francesco@gadaleta.org
ICQ : 286063291
Fingerprint: 3CB8 C721 FE99 BC98 38DC F463 BFDC E4EC CE3B 7327 (1024/DSA) [with image]
Linux Registered User #327326
________________________________________________
Non speditemi documenti in formato Word
o Powerpoint.
RMS vi spiega il motivo qui:
http://www.gadaleta.org/allegati.html
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: assignment makes pointer from integer without a cast?
2004-12-13 16:13 ` Francesco Gadaleta
@ 2004-12-13 19:53 ` Jan-Benedict Glaw
2004-12-16 2:07 ` how to link mqueue library Ron Michael Khu
0 siblings, 1 reply; 5+ messages in thread
From: Jan-Benedict Glaw @ 2004-12-13 19:53 UTC (permalink / raw)
To: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1633 bytes --]
On Mon, 2004-12-13 17:13:54 +0100, Francesco Gadaleta <francesco@gadaleta.org>
wrote in message <20041213161354.GD1487@sweetdream>:
> > void usage(char *prog_ptr) /* not int* */
> > {
> > fprintf(stdout,"progname is: %s\n",prog_ptr); /* not
> > *prog_ptr */
> > }
> >
> > int main(int argc, char *argv[])
> > {
> > char *prog_ptr; /* not int * */
> > prog_ptr = basename (argv[0]);
> > usage(prog_ptr); /* not &prog_ptr */
> > return(0);
> > }
> change this line:
> prog_ptr = basename (argv[0]);
>
> with this:
> prog_ptr = (char*) basename (argv[0]);
No, better include the proper header file <libgen.h> or <string.h>
(after #defining _GNU_SOURCE). This way, you don't force gcc into
guessing it's type. If you had compiled this with a C++ compiler (*), it
wouldn't even had accepted the code or wouldn't link it later on.
MfG, JBG
(*) Compiling not-too-complicated C code with a C++ compiler can reveal
some additional oddities that a pure C compiler wouldn't warn on. So if
you keep your code being compileable with both a C and a C++ compiler
when you start a new project, this won't hurt (but probably pay back at
some time). (-> Right, I'm a C programmer and personally don't use C++
that often, because it's too hard to write "basic" stuff with it).
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-12-16 2:07 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-13 0:52 assignment makes pointer from integer without a cast? Patrick
2004-12-13 1:47 ` Hossein Mobahi
2004-12-13 16:13 ` Francesco Gadaleta
2004-12-13 19:53 ` Jan-Benedict Glaw
2004-12-16 2:07 ` how to link mqueue library Ron Michael Khu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).