how to reinterpret cast of "integer pointer" to "char array?

Technical questions regarding the XTC tools and programming with XMOS.
jerryXCORE
Experienced Member
Posts: 65
Joined: Tue Apr 30, 2013 10:41 pm

how to reinterpret cast of "integer pointer" to "char array?

Post by jerryXCORE »

In XMOS, we can reinterpret cast of integer array to char array as:

Code: Select all

int data[100];   
char byte = 0;
(data, char[])[5] =  byte;
But how can we reinterpret cast of integer pointer to char array?

Code: Select all

int data[100], *ptr = & data[0];   
char byte = 0;
(*ptr, char[])[5] =  byte; //  This doesn't work: "error: out of bounds array access"
Thanks!
Last edited by jerryXCORE on Fri Dec 04, 2015 9:20 pm, edited 1 time in total.


richard
Respected Member
Posts: 318
Joined: Tue Dec 15, 2009 12:46 am

Post by richard »

Code: Select all

int data[100], *ptr = & data[0];   
char byte = 0;
(*ptr, char[])[5] =  byte; //  This doesn't work: "error: out of bounds array access"
sizeof(*p) == 4. As a result you can only reinterpret it to a type of size 4 or less. If you reinterpret it as a array of char then the array will only have 4 elements, hence the out of bounds error.

In this case I'd suggest using a C-style cast to convert from int * to char *, i.e the following should work:

Code: Select all

((char *)ptr)[5] = byte;
jerryXCORE
Experienced Member
Posts: 65
Joined: Tue Apr 30, 2013 10:41 pm

Post by jerryXCORE »

Yes, it works, thanks a lot!