Find the answer to your Linux question:
Results 1 to 5 of 5
Here is the code I wrote to detect whether a LAN cable is pluged or unpluged. After compilation, it gives me the warning: warning: assignment makes integer from pointer without ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25

    "warning: assignment makes integer from pointer without a cast" in C

    Here is the code I wrote to detect whether a LAN cable is pluged or unpluged. After compilation, it gives me the warning:

    warning: assignment makes integer from pointer without a cast
    Here is my code:

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    #define yes 1
    #define no 0
    typedef int boolean

    int main()
    {
      
    FILE *fp;
      
    char *pch;
      
    char line[130];            /* line of easa!from unix command*/
      
    int m;
      
    boolean b;
       
      
    fp popen("nm-tool | grep Link""r");        /* Issue the command.        */

                        /* Read a line            */
      
    while ( fgetslinesizeof linefp))
      {
        
    printf("%s"line);
      }
      
    pclose(fp);

      
    pch strtok (line,"     ");
      for (
    m=0m<2m++)
      {
        
    pch strtok (NULL"     ");
      }
        
    printf ("%s\n",pch);
      
    pch;
      
    printf("%d\n",b);        /* more to add on */

      
    return 0;


    I have googled some solutions online, like include the stdlib.h header and etc but all didn't work. Does anyone has any idea on it?

    Thanks in advance.

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    i'm not a C programmer, but it seems to me that you are trying to assign a char pointer to a boolean with b = pch, when pch is a character pointer and b is a boolean

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25
    thanks coopstah13. The reason i did so is because the pch i get in the end will be either yes or no and since i had defined yes and no to 1 and 0 respectively before the main program, that is why i assigned it to a boolean with b=pch.

    Even though i also agree with you that the warning is most probably because of this assignment, but I have no idea on how to solve it as I'm a beginner to C programming.

  4. #4
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by christyyim View Post
    thanks coopstah13. The reason i did so is because the pch i get in the end will be either yes or no
    Who said that?
    strtok will return a pointer to char. Therefore, pch will be of the value NULL or non NULL. Finding Tokens in a String - The GNU C Library

    So if you want b to be 1 in case pch is non-null, you need something like

    b = (pch != NULL);
    Debian GNU/Linux -- You know you want it.

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    25
    yah, GNU-Fan, you hit the nail on the head!!! Thanks

    I misunderstood the way how strtok works. And now i use strstr function instead of strtok. Thanks.

Posting Permissions

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