Find the answer to your Linux question:
Results 1 to 6 of 6
Linux format have started a new programming academy which is a c primer. Very cool and a good way to get another perspective. The first code is a basic shell! ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,569

    How should I cast...


    Linux format have started a new programming academy which is a c primer. Very cool and a good way to get another perspective.

    The first code is a basic shell!

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    int parse(char **args)
    {
    
        static char line[100];
        printf("> ");
        if (gets(line) == NULL)
            return -1;
    
    
        *args++ = strtok(line, " \t");
        while (*args++ = strtok(NULL, " \t"));
    
        return 1;
    }
    void main()
    {
        char *args[20];
        while(parse(args) > 0) {
            if (fork() == 0) {
                execvp(args[0], args);
                printf("%s not found\n", args[0]);
                exit(1);
            } else {
                wait(0);
            }
        }
    
    }
    I mostly understand this but... the lines in red generate warnings:

    warning: assignment makes pointer from integer without a cast

    So reading the strtok documentation I see that it returns a char * so I cast to char *

    Code:
     *args++ = (char *)strtok(line, " \t");
        while (*args++ = (char *)strtok(NULL, " \t"));

    They generate a different warning:

    warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

    What should I cast to so that I get rid of the warnings?
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    With my gcc version It gave a bonus warning message

    $ gcc elija.c
    elija.c: In function ‘parse’:
    elija.c:14: warning: assignment makes pointer from integer without a cast
    elija.c:15: warning: assignment makes pointer from integer without a cast
    /tmp/cchURIpq.o: In function `parse':
    elija.c.text+0x24): warning: the `gets' function is dangerous and should not be used.
    $ gcc --version
    gcc (GCC) 4.4.4 20100630 (Red Hat 4.4.4-10)
    Copyright (C) 2010 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions. There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    Googling around a bit came across suggestion - Turn on more warning with "-Wall option"
    gave this
    [laks@space ~]$ gcc elija.c -Wall
    elija.c: In function ‘parse’:
    elija.c:14: warning: implicit declaration of function ‘strtok’
    elija.c:14: warning: assignment makes pointer from integer without a cast
    elija.c:15: warning: assignment makes pointer from integer without a cast
    elija.c:15: warning: suggest parentheses around assignment used as truth value
    elija.c: At top level:
    elija.c:19: warning: return type of ‘main’ is not ‘int’
    elija.c: In function ‘main’:
    elija.c:28: warning: implicit declaration of function ‘wait’
    /tmp/ccIHuq0E.o: In function `parse':
    elija.c.text+0x24): warning: the `gets' function is dangerous and should not be used.
    I did man strtok seems like it needs string.h which is missing with your code.
    Added #include <string.h> then ran without -Wall now the warning disappeared

    Thanks to your code - I learned about new method to get rid of warnings , previously I used to ignore them.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  3. #3
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,569
    Thanks Laks, I didn't even think about needing another header as the code works as printed. Loving the -Wall option as I was taught in college that warnings are basically errors that need to man up. So I'm having a go at fixing them up now.

    Oh and

    Code:
    richard@andromeda shell $ gcc -v
    Using built-in specs.
    COLLECT_GCC=gcc
    COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.1-4' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-multiarch --with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib/x86_64-linux-gnu --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
    Thread model: posix
    gcc version 4.6.1 (Debian 4.6.1-4)
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  4. #4
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    I find it strange that gcc version 4.6.1 didn't show the "gets dangerous" warning message but gcc 4.4.4 shows it ?
    Elija,you may be interested in reading more about it here - c - warning:gets function is dangerous - Stack Overflow
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

  5. #5
    Penguin of trust elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    3,569
    That's an interesting read. Thanks.

    I also read the fgets manpage and it says that gets is deprecated, so I wonder if gcc no longer issues a warning? I can't find anything on Google about it though.

    Final code

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    
    //  Missing headers
    #include <string.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int parse(char **args)
    {
    
    	static char line[100];
    	int l = 0;
    	
    	printf("> ");
    	if (fgets(line, 100, stdin) == NULL)
    		return -1;
    
    	//  There is probably a simpler way of removing the carriage return
    	l = strlen(line);
    
    	if (10 == *(line + (l-1))) {
    		*(line + (--l)) = '\0';
    	}
    
    	// cast strtok return to char *
    	*args++ = (char *)strtok(line, " \t");
    
    	// cast strtok return to char *
    	// put paranthesis around assignment
    	while ((*args++ = (char *)strtok(NULL, " \t")));
    
    	return 1;
    }
    
    //  main should return int
    int main()
    {
    	char *args[20];
    	while(parse(args) > 0) {
    		if (fork() == 0) {
    			execvp(args[0], args);
    			printf("%s not found\n", args[0]);
    			exit(1);
    		} else {
    			wait(0);
    		}
    	}
    
    	//  main should return int
    	return 0;
    }
    Last edited by elija; 08-29-2011 at 08:41 AM.
    What do we want?
    Time machines!

    When do we want 'em?
    Doesn't really matter does it!?


    The Fifth Continent

  6. #6
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,758
    Okay. I checked above code ,Its fine with my gcc version too.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •