Thread: pointer from integer without a cast - warning

  1. #1
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9

    pointer from integer without a cast - warning

    Hi,

    Could you please help me with my problem?

    I have the following structure:
    Code:
    static struct device_t * dev;
    int err = close(dev);
            if (err != 0)
            {
                LOGE(err, "couldn't close device (%s)", strerror(-err));
             }
    LOGD("Close device OK");
    The warning: "passing argument 3 of '__android_log_print' makes pointer from integer without a cast" is shown at
    Code:
    LOGE(err, "couldn't close device (%s)", strerror(-err));
    Thank you.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well how is LOGE() defined? And why are you passing -err to strerror()?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As far as I understand from reading documentation, strerror returns an address to a string. Seems your compiler warning sees it returning an integer.
    Did you remember to include
    #include <string.h> // for strerror()

  4. #4
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    @itsme86: should I have used instead LOGE_IF()? I'm passing -err to strerror, because I want to print the real error message
    @nonoob: actually, I haven't included
    #include <string.h>.

  5. #5
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    But I also get the same error when trying the following:
    #include <string.h>
    .....
    char *value;
    value=get_val("FORMAT");
    ....
    where get_val is declared as: int get_val(char *variable); and "FORMAT" is declared by the user

    What can be so wrong that this error keeps popping out?

    Thank you.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Andreea Coman View Post

    char *value;
    value=get_val("FORMAT");
    ....
    where get_val is declared as: int get_val(char *variable); and "FORMAT" is declared by the user

    What can be so wrong that this error keeps popping out?
    Well, your function returns an int and you're trying to assign it to a char *.

  7. #7
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    About the second issue:
    @anduril462: but if I have several strings declared by the user, such as "ROTATION" or "HEIGHT" which take integer values, then how could I use the same get_val function for them and for "FORMAT" (which takes char values)?

    Thank you.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by Andreea Coman View Post
    @itsme86: should I have used instead LOGE_IF()? I'm passing -err to strerror, because I want to print the real error message
    @nonoob: actually, I haven't included
    #include <string.h>.
    err will always be -1 on error and will more than likely your error will not be 1. Have a look at close and see that it sets errno.
    also close takes an int argument not a struct device_t *.
    Code:
    NAME
           close - close a file descriptor
    
    SYNOPSIS
           #include <unistd.h>
    
           int close(int fd);
    
    DESCRIPTION
           close()  closes  a  file descriptor, so that it no longer refers to any
           file and may be reused.  Any record locks (see fcntl(2))  held  on  the
           file  it  was  associated  with,  and owned by the process, are removed
           (regardless of the file descriptor that was used to obtain the lock).
    
           If fd is the last copy of a particular file  descriptor  the  resources
           associated  with it are freed; if the descriptor was the last reference
           to a file which has been removed using unlink(2) the file is deleted.
    
    RETURN VALUE
           close() returns zero on success.  On error, -1 is returned,  and  errno
           is set appropriately.

  9. #9
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    @sparkomemphis:
    Actually my close is defined by the user as being something like:
    static inline int close(struct device_t* device);

    Sorry for not mentioning it before !!

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by Andreea Coman View Post
    @sparkomemphis:
    Actually my close is defined by the user as being something like:
    static inline int close(struct device_t* device);

    Sorry for not mentioning it before !!
    That's not a good idea since there is already a function called closed that is needed to close files. Depending up on the OS and linker you may be one or the other.

  11. #11
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    @sparkomemphis:
    I have renamed the close() function into my_close(), and still get the same error

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Try (assuming your my_close() returns 0 on failure):

    Code:
    static struct device_t * dev;
    int err = my_close(dev);
    if (err != 0)
    {
      char error_buffer[BUFSIZ];
      sprintf(error_buffer, "couldnt' close device (%s)", strerror(err));
      LOGE(error_buffer);
    }
    LOGD("Close device OK");
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Have you defined LOGE someplace? As prototype or perhaps you are missing another #include for its header.

  14. #14
    Registered User
    Join Date
    Jan 2012
    Location
    Galati, Romania
    Posts
    9
    @nonoob: I have used LOGE simple in a format like: LOGE("Can't open this !"); and no errors appeared, so I'm guessing it's something wrong with the pointer-integer issue. I will try what itsme86 suggested and let you know.

    Any suggestions for the same error, but problem no.2? :-s

    Thanks guys

  15. #15
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Andreea Coman View Post
    @anduril462: but if I have several strings declared by the user, such as "ROTATION" or "HEIGHT" which take integer values, then how could I use the same get_val function for them and for "FORMAT" (which takes char values)?
    Do you have some sort of key-value pair, where "FORMAT" may correspond to a value of "foo" and "HEIGHT" may correspond to a value of "3"? Then you need to either make a functions called get_int_val and get_str_val, or you need to make get_val return the char * version of the data and use a function like strtol to convert the result of get_val to a long if it's supposed to be a number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-12-2011, 07:33 PM
  2. Replies: 4
    Last Post: 03-03-2010, 01:06 PM
  3. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  4. Replies: 1
    Last Post: 12-23-2008, 09:39 AM
  5. warning: cast to pointer from integer of different size
    By DavidDobson in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 06:37 PM

Tags for this Thread