Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #25327

Re: pointer from integer without a cast

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: pointer from integer without a cast
Date 2012-08-28 18:47 -0400
Organization A noiseless patient Spider
Message-ID <503D4A71.9060102@verizon.net> (permalink)
References <2163629c-1e5f-4b19-a97e-3dc0497b760f@googlegroups.com>

Show all headers | View raw


On 08/28/2012 05:56 PM, Rudra Banerjee wrote:
> Dear friends,
> I am a C novice. I am trying to get the basename of a file(filename) without extension. The problem is much discussed in net, but due to my limited knowledge, I failed to make fruitful use of them, and thought I may use the unix command, as given.
> 
>       char myCommand[512];
> //         char *basetmp=strrchr(filename,'/')+1;
>     sprintf(myCommand,"basename %s .bib",filename);
>     char *basefn=system((char *)myCommand);
>     printf (basefn);
> variable filename is defined as char*.
> This code is giving warnning:308:18: warning: initialization makes pointer from integer without a cast [enabled by default]
> where line no 308 is  "char *basefn=system((char *)myCommand);"
> 
> If anybody kindly show me where I am getting wrong,

The value returned by system() is an int. Your code tries to implicitly
convert that int to a pointer, which is not permitted, hence the warning
message. Such conversions can only be done explicitly by using a cast.
Inserting (int) before system() would disable the warning message, but
would not solve your problem. The value returned by the system() command
when it is given a non-null pointer argument is an
implementation-defined status code, so converting it to a pointer is
pointless, and could cause your program to crash.

I'm familiar with the basename command as a standard utility available
on unix-like systems like the ones I use at home and at work. I've not
idea whether other systems provide a similar command with the same name.
The rest of this message is specific to my desktop system, and if not
applicable to your system, should be ignored. If the advice is
applicable to your system, any questions should be directed to
comp.programmer.unix, rather than this forum. There are people there who
can give you better answers than I can about such systems.

system() returns  "-1 on error (e.g.  fork() failed), and the return
status of the command otherwise. This latter return status is in the
format specified in wait(2)." That's not what you want.

What you really want is to capture the string that gets printed out by
the basename command, but capturing that string is a complicated task
that would start with using popen() rather than system(), and gets
steadily more complicated from there. I'd recommend someday learning how
to do that, because someday you might need to, but this isn't that day;
wait until you no longer call yourself a "novice".

You're much better off using the basename() function, rather than
system() and the basename command. Type "man 3 basename" at your command
line for more information.

Back to comp.lang.c | Previous | Next | Find similar | Unroll thread


Thread

Re: pointer from integer without a cast James Kuyper <jameskuyper@verizon.net> - 2012-08-28 18:47 -0400

csiph-web