1

I have a serial port which transmit 256 Byte data over SCI protocol.

The frame has following format:

  typedef struct
  {
     int header;
     uint8 ssap;
     uint8 dsap;
     uint8 data[248];
  }Frame;

And have following function

  void sciSendStream(int length, uint8* data);

My question is how to convert Frame struct to uint8 *

When i try to do that with the following cast:

  sciSendStream(256,(uint8 *)frame);

I am receiving the incompatible conversion error.

Nazim
  • 406
  • 1
  • 6
  • 20
  • @Jabberwocky Doh! – Andrew Henle Nov 05 '20 at 13:18
  • What is "SCI protocol"? The only SCI I know of is Motorola's name for UART hardware. – Lundin Nov 05 '20 at 13:42
  • `void sciSendStream(int length, uint8* data);` should have `int sciSendStream(const size_t length, const void *data);` prototype if it was written by sane programmer. – 0___________ Nov 05 '20 at 13:42
  • @Lundin Yes It is a kind of UART protocol – Nazim Nov 05 '20 at 13:47
  • @Nazim you need to learn pointers and how to use them: https://www.tutorialspoint.com/cprogramming/c_pointers.htm – 0___________ Nov 05 '20 at 13:48
  • @P__JsupportswomeninPoland since this is a legacy code which i cannot modify the parameters was defined with const specifier. Refactoring needed thanks – Nazim Nov 05 '20 at 13:49
  • @Nazim I can only imagine how bad this code is inside judging only form the prototype :) – 0___________ Nov 05 '20 at 13:50
  • 1
    No it is not a _protocol_, it is a piece of hardware sitting in your MCU. – Lundin Nov 05 '20 at 13:50
  • @P__JsupportswomeninPoland Tutorialspoint is [not recommended](https://stackoverflow.com/questions/62816217/are-the-c-mock-tests-at-tutorialspoint-correct). It's all written by some random student. – Lundin Nov 05 '20 at 13:52
  • @Lundin still good enough for someone who never heard about pointers\ – 0___________ Nov 05 '20 at 13:53
  • @P__JsupportswomeninPoland I'd rather recommend newbies to read a book written by someone with actual domain expertise. [Modern C](https://modernc.gforge.inria.fr/) is a decent one and available for free. – Lundin Nov 05 '20 at 13:56
  • @Lundin thank you very much for your suggestion sir and positive critics – Nazim Nov 05 '20 at 14:01

1 Answers1

4

You cannot convert frame into uint8* because frame is not a pointer but a Frame. But you can convert the address of frame (which is a pointer) to a uint8* by using the & operator.

Just replace:

sciSendStream(256, (uint8*)frame);

with:

sciSendStream(256, (uint8*)&frame);`. 

I assume unint8 is unsigned char.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • the very first member of `Frame` is `int`, so you are casting `int` to `uint8`. Is this intended / legal? – Stephan Lechner Nov 05 '20 at 13:29
  • well better `sciSendStream(sizeof(Frame), (uint8*)&frame);` – Stefan Riedel Nov 05 '20 at 13:32
  • @StephanLechner, actually it's just casting a pointer to another type of pointer which is perfectly legal. – Jabberwocky Nov 05 '20 at 13:33
  • @StefanRiedel maybe, but only the OP knows this exactly. – Jabberwocky Nov 05 '20 at 13:33
  • If we use the non-dysfunctional `uint8_t` instead of home made `uint8`, the code is fine. `uint8_t` will be a character type on all known systems, and can therefore be used to iterate through any form of data. What `uint8` is however, is anyone's guess. – Lundin Nov 05 '20 at 13:48
  • @Lundin I would rather suggest `const void *`, and `const size_t` for length. The function should also return some status :). – 0___________ Nov 05 '20 at 13:54
  • 1
    @P__JsupportswomeninPoland The advantage with `const uint8_t*` over `const void*` is type safety. And you can use pointer arithmetic on `const uint8_t*`, meaning it can be treated like an array. – Lundin Nov 05 '20 at 13:59
  • @Lundin what type safety pointer converison from void * and to void * is safe – 0___________ Nov 05 '20 at 14:03
  • 1
    @P__JsupportswomeninPoland It's not the actual pointer conversion, it is when you de-reference whatever the void pointer points at. Then you instantly get all manner of concerns about size, alignment, pointer aliasing, trap representations etc etc. – Lundin Nov 05 '20 at 14:54
  • @Lundin pointer -> void pointe -> pointer to char is same safe as uint8_t pointer. – 0___________ Nov 05 '20 at 15:51