Pointer casting during function call
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-08-22 3:41 PM
Hello,
What's the difference between the following 2 function calls ?
some_function ( & x ) ;
some_function ((uint8_t *)&x) ;
- Labels:
-
DEBUG
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-08-22 4:04 PM
Technically in compiled code there is no difference. With this cast you are just telling the compiler that this conversion of different pointer types really is OK and there is no need for a warning. :)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-08-22 4:37 PM
Warning depends on how x is defined
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-08-24 12:15 PM
So it should be used when x is a point - but IS NOT a pointer to uint8_t ?
The cast makes x to point to uint8_t - so there's no warning ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-08-24 3:14 PM
Is is not clear from your case WHAT x is.
Probably shouldn't be a pointer because you're asking what address the variable is in memory, ie &x (Address of x)
uint32_t x;
some_function ((uint8_t *)&x) ; // casting the address of 32-bit value x, to a byte pointer
Up vote any posts that you find helpful, it shows what's working..
