1

I have a pointer to the array of structs:

struct myStruct (*str_ptr)[];

I try to assign to it a value i have in another pointer which type is (uint8_t*) When I do:

uint8_t* ptr2;
ptr2 = (some memory adress);
str_ptr = ptr2;

I get a warning: "assignment to 'myStruct (*)[]' {aka 'struct <anonymous> (*)[]'} from incompatible pointer type 'uint8_t *' {aka 'unsigned char *'} [-Wincompatible-pointer-types]"

Obvious solution to the problem is to cast ptr2 to the type of str_ptr... and I tried many configurations, but all casts I can think of returns in compiler error. Anyone know how to cast a pointer to the "pointer to the array of structs"?

Edit: Sorry, I will try to describe what I'm trying to do here. I have some data on external memory, and firstly I declare memory for it:

ptr2 = malloc(data_size);

Then fill it with some data using memcpy (thats why I want it to be (uint8_t*) type). Lastly, I try to assign my pointer to the array of structs to the place where the data starts, so i can access it by typing (*str_ptr)[3].field for example.

anon t
  • 13
  • 4
  • A typical pointer to an array of structs (or anything, for that matter) would be a simple pointer to struct, like `struct myStruct *str_ptr;`. And the best way to assign to your struct from untyped memory is with `memcpy()`, e.g. `memcpy(str_ptr, ptr2, sizeof(*str_ptr);`. – Peter - Reinstate Monica Sep 29 '22 at 14:11
  • You can't wildly convert from `uint8_t*` to another pointer type, simple as that. Casting means you might invoke undefined behavior = compiler error or no compiler error, it's still a bug. What is the actual problem you are trying to solve here? – Lundin Sep 29 '22 at 14:11
  • @Lundin Such problems typically arise when data is received by some hardware. – Peter - Reinstate Monica Sep 29 '22 at 14:14
  • @AdrianMole I believe the Linux kernel performed a lot of such casts and simply switched gcc to non-anal mode by optioning off strict aliasing. Linus famously ranted about compilers making these assumptions, but that was half a lifetime ago. Not sure what the current state of affairs is -- given how natural it is to impose a layout on data which you know has that layout, and how well it actually fits the original loose C type paradigm I'd bet it's still common. It feels a lot like C was meant to not only allow but *facilitate* such casts. – Peter - Reinstate Monica Sep 29 '22 at 14:19
  • See: https://stackoverflow.com/q/98650/10147399 – Aykhan Hagverdili Sep 29 '22 at 14:23
  • 1
    @Peter - Reinstate Monica I receive data from external FRAM memory. Also, all structs I use are with `__attribute__((packed))` – anon t Sep 29 '22 at 14:38

1 Answers1

0

Pointers to arrays can present tricky C syntax issues. However, a very strong hint for the type of cast required to avoid a compiler warning is given in the message you have quoted: "assignment to 'myStruct (*)[]'

However, that type isn't quite complete, because you also need the struct keyword.

So, your warning-free cast assignment would be like this:

str_ptr = (struct myStruct(*)[])ptr2;
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I was looking at it and I'm pretty sure I tried this cast, but must made some typo and just think "It does not work, there should be some other way". Thank you for your answer anyway! – anon t Sep 29 '22 at 14:40