Thread: warning: cast to pointer from integer of different size

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    58

    warning: cast to pointer from integer of different size

    Hi

    I'm sure this is a simple problem but I'm quite stuck, when ever I compile my code I get the "warning: cast to pointer from integer of different size" warning. Could someone show me how to cast/point or address the variable to stop it, thanks

    The WriteUserData function requires a uint8_t data array type to work.

    Code:
    typedef struct {
      
    	uint64_t MacAddress;
    	uint64_t RootMacAddress;
    	uint16_t PANId;
    	uint8_t device_Type;
    
    } PACK configData_t;
    Code:
    int setConfigData(configData_t *cfg_dta) {
    
    	uint8_t success = 0;
    
    	if (WriteUserData(0, (uint8_t*)cfg_dta->MacAddress, 8, (void*)WriteCallback) != SUCCESS) {
    		success = -1;
    	} // if
    
    return success;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, do you actually want to use MacAddress as the address of a location in memory, or do you want to pass the location containing MacAddress as a pointer?
    Code:
    (uint8_t*)cfg_dta->MacAddress
    makes MacAddress - as it is - into a pointer.
    Code:
    (uint8_t*)&cfg_dta->MacAddress
    Makes the location in memory of MacAddress into a uint8_t pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    58
    ah ha! option number two works, many thanks - I was really stuck on that!



    David

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by DavidDobson View Post
    ah ha! option number two works, many thanks - I was really stuck on that!



    David
    beware that sometimes "Work != Correct", but I guess in this case "Work == Correct".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Some warnings in C are not really warnings, but errors. That is semantically speaking, since they are compiler warnings, but are semantically incorrect.
    Remember that well. This is especially true when pointers are involved. If the code is correct, then you will get no warnings at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Some warnings in C are not really warnings, but errors. That is semantically speaking, since they are compiler warnings, but are semantically incorrect.
    Remember that well. This is especially true when pointers are involved. If the code is correct, then you will get no warnings at all.
    Well, in this case, it's a warning because the compiler says "I can do this, but I doubt it's what you wanted". Warnings aren't a good idea - it usually means that you are doing something wrong, just like an error. But it's not an error, because the compiler IS capable of actually doing what is asked for. For example, a 64-bit integer can hold a 32-bit pointer without any trouble at all - so a cast from a 64-bit integer to a 32-bit pointer COULD be correct code - it may have been written on a system where pointers are 64-bit, and thus needed to be stored in a 64-bit integer [ok, so we can debate whether storing pointers as integers is EVER a good idea, but unfortunately, some people do think it is a good idea, so we can't just forbid such casts].

    In C++, you would probably want to use a reinterpret_cast<uint8_t *>(...) to do the same thing, and I expect that Visual Studio gives exactly the same warning if you put a 64-bit integer in () on such cast - but it is perfectly valid C++ code in all other respects [assuming of course that we end up with a valid address in the pointer if the pointer is ever dereferenced].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, hence semantically incorrect, but compiler correct.
    Just because they are warnings does not mean your code is correct. One should beware of warnings, that's all I wanted to add. Especially in C.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-10-2009, 10:29 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM