Assignment make pointers
If you take a look in "man 3 basename", you'll see that basename function
just returns a pointer to a char. In the case above you need to "cast" the pointer returned to be of type int.
Like: prog_ptr = (int *) basename (argv[0]);
I hope it will help you.
Good luck.
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);
} On Mon, 2004-12-13 08:04:32 -0200, r_zaca <[email protected]> wrote in message <[email protected]>:
Content-Description: Mail message body
Quoted 4 lines
If you take a look in "man 3 basename", you'll see that basename function
just returns a pointer to a char. In the case above you need to "cast" the pointer returned to be of type int.
Like:prog_ptr= (int *) basename (argv[0]);
Don't do that. Usually, you can write even quite large programs without using casts at all. Most of the time, a cast points to spots in your programs that are badly designed from the type of your variables point of view. If your variables are of senseful types and obey a well-thought structure (like using enums and unions instead of #defines and casts), you usually don't need to cast.
Also, I usually suggest to compile code with "-Wall" -- that'll show you probably lots of odd constructs that should be written differently (or using proper types).
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));
Quoted 9 lines · Jan-Benedict Glaw
On Mon, 2004-12-13 08:04:32 -0200,
r_zaca<[email protected]> wrote in message <[email protected]>:
Content-Description: Mail message bodyQuoted 4 lines
If you take a look in "man 3 basename", you'll see that basename
function just returns a pointer to a char. In the case above you need to "cast" the pointer returned to be of type int.
Like:prog_ptr= (int *) basename (argv[0]);
Quoted 9 lines
Don't do that. Usually, you can write even quite large programs without using casts at all. Most of the time, a cast points to spots in your programs that are badly designed from the type of your variables point of view. If your variables are of senseful types and obey a well-thought structure (like using enums and unions instead of #defines and casts), you usually don't need to cast.
Also, I usually suggest to compile code with "-Wall" -- that'll show you probably lots of odd constructs that should be written differently (or using proper types).
True enough. The problem intrigued me enough that I test compiled with -Wall and the compiler complains of an implicit declaration of basename. It seems he was missing #include <libgen.h>. Adding this cleared everything up.
What escapes me though is how the compiler could compile without knowing the definition. Isnt it REALLY presumptuous and dangerous for the compiler to assume function definitions? I assume basename is in the standard c library though or it certainly wouldnt link. Can anyone with more experience elaborate?
Quoted 1 line
MfG, JBG
Signature · 1 line
-EB
Quoted 5 lines · Eric Bambach
What escapes me though is how the compiler could compile without knowing the definition. Isnt it REALLY presumptuous and dangerous for the compiler to assume function definitions? I assume basename is in the standard c library though or it certainly wouldnt link. Can anyone with more experience elaborate?
Sorry to reply to my own post, but nevermind. Its too early (someone called and woke me up :) and I've been programming in C++ too long where prototypes are required. gcc will compile happily without libgen.h while g++ won't. That answers my own question.
Sorry for the noise.
Signature · 1 line
-EB
C Newbie
I need to know (before I get flamed) if this is an appropriate spot to post questions on C programming or not and the very most newbie level. I'm attempting a transition from Perl and, while I understand a lot of the fundamentals of C, am having a hard time with string (char) manipulation.
Thanks,
- Darren
Re: C Newbie
On Mon, 2004-12-13 10:01:28 -0500, Darren Sessions <[email protected]> wrote in message <BDE318F8.1618%[email protected]>:
Quoted 4 lines
I need to know (before I get flamed) if this is an appropriate spot to post questions on C programming or not and the very most newbie level. I'm attempting a transition from Perl and, while I understand a lot of the fundamentals of C, am having a hard time with string (char) manipulation.
Start asking. However, start a question with a new email, not by pressing you email client's "reply" button and deleting any old text. Hijacking threads is bad habit and makes proper email sorting a hard challange...
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));
Re: C Newbie
In reply to Darren SessionsQuoted 8 lines · you
I need to know (before I get flamed) if this is an appropriate spot to post questions on C programming or not and the very most newbie level. I'm attempting a transition from Perl and, while I understand a lot of the fundamentals of C, am having a hard time with string (char) manipulation.
Thanks,
- Darren
As JBG advised yes please don't hijack threads. It confuses me AND my mailer. Other than that I would be happy to assist on the list if I can, even though my programming skills aren't great yet. My advice is to limit your questions though. Try to do most of the reasearch off list and ask specifc questions when something stumps you or doesnt work as expected after you've tried to debug it yourself. If it looks like you didn't try (ex. throwing code at us with the subject it doesnt compile) or cant give us specific code with questions to help on you're less likely to get any useful information off the list.
Other than that, I hope I can be of help and I hope the list gurus agree with me.
Signature · 1 line
-EB
Defunct Processes
In reply to Darren SessionsThe application I'm working on right now gets called from another application. When my program is finished, I just use exit(0); to quit, but if I do a ps there are a ton of [applicationname] <defunct> messages.
I'm using Suse Enterprise Linux 9.0 with the 2.6 Kernel.
Thanks,
- Darren
Re: Defunct Processes
On Tue, 2004-12-28 21:13:56 -0500, Darren Sessions <[email protected]> wrote in message <BDF77D14.1AE7%[email protected]>:
Quoted 3 lines
The application I'm working on right now gets called from another application. When my program is finished, I just use exit(0); to quit, but if I do a ps there are a ton of [applicationname] <defunct> messages.
This is a fault of the calling application. Your program became a zombie. That's the remaining rest of a process that already finished. (Basically, it consists of accounting information, the process' exit value and probably of the signal number that led to the process' death).
This information needs to be handled (the parent process needs to wait() or wait4() it, or using other methods). Normally, the parent process should handle the SIGCHLD signal; it's generated upon a child's death. This would allow the parent to call wait() as long as there are finished childs (remember, you only know the signal got fired, but you don't know how often :) Calling wait() then will finally bury the finished child.
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));
Re: Defunct Processes
you can
signal(SIGCHLD, SIG_IGN);
to ignore childs termination notifications, so, the parent dont need to wait() for childs, they just go without any parent notification, and no zombies.
On Mon, 2004-12-13 08:51:44 -0600, Eric Bambach <[email protected]> wrote in message <[email protected]>:
Quoted 2 lines
What escapes me though is how the compiler could compile without knowing the definition. Isnt it REALLY presumptuous and dangerous for the compiler to
The "type defaults to int" thing. This is why the initial writer got an error message that an int type is about to be converted to pointer type.
MfG, JBG
Signature · 4 lines
Jan-Benedict Glaw [email protected] . +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));