UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Observer obernikhin
Observer
4,766 Views
Registered: ‎09-01-2009

C array index problems during type cast.

Jump to solution
EDK 11.2
I have a char array DataArr,and integer value FlagData.
I need to write Flag data to some possition in DataArr.
unsigned int * FlagDataPtr=NULL;
..............................................................
FlagDataPtr=(unsigned int *)&DataArr[Pos];
*FlagDataPtr=FlagData;
If Pos isn't divisible by 4 a FlagDataPtr point to nearest smaller and divisible by 4 index of DataArr;
Example Pos=14, but FlagDataPtr bind to 12 element of DataArr.
Where is error?
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
Highlighted
Explorer
Explorer
5,793 Views
Registered: ‎08-29-2008

Re: C array index problems during type cast.

Jump to solution

Hi,

 

I think, that issue is a feature, not an error of the GCC!

I would implement an other scenario.

 

uint32 iDataArr[n];

uint8  *cDataArrPtr=(uint8*)&iDataArr[0];

int pos; // 0...n

 

(cDataArrPtr+=4  == iDataArr++)

 

To get each Byte of iDataArr:

cDataArrPtr=&iDataArr[pos];

byte0=(uint8)*(cDataArrPtr+0);

byte1=(uint8)*(cDataArrPtr+1);

byte2=(uint8)*(cDataArrPtr+2);

byte3=(uint8)*(cDataArrPtr+3);

 

It's more  reasonable (for me) ;-)

 

Rgds,

Kai

View solution in original post

0 Kudos
3 Replies
Historian
Historian
4,756 Views
Registered: ‎02-25-2008

Re: C array index problems during type cast.

Jump to solution

obernikhin wrote:
EDK 11.2
I have a char array DataArr,and integer value FlagData.
I need to write Flag data to some possition in DataArr.
unsigned int * FlagDataPtr=NULL;
..............................................................
FlagDataPtr=(unsigned int *)&DataArr[Pos];
*FlagDataPtr=FlagData;
If Pos isn't divisible by 4 a FlagDataPtr point to nearest smaller and divisible by 4 index of DataArr;
Example Pos=14, but FlagDataPtr bind to 12 element of DataArr.
Where is error?

What is question?

----------------------------Yes, I do this for a living.
0 Kudos
Observer obernikhin
Observer
4,745 Views
Registered: ‎09-01-2009

Re: C array index problems during type cast.

Jump to solution
>>What is question?
Question is: is this an compiller bug, or just some compiller parameter setting?
Coz compiller automaticly bind position of intger pointer to nearest smaller and divisible by 4 index of array.
I don't need this automaticly index shift.
0 Kudos
Highlighted
Explorer
Explorer
5,794 Views
Registered: ‎08-29-2008

Re: C array index problems during type cast.

Jump to solution

Hi,

 

I think, that issue is a feature, not an error of the GCC!

I would implement an other scenario.

 

uint32 iDataArr[n];

uint8  *cDataArrPtr=(uint8*)&iDataArr[0];

int pos; // 0...n

 

(cDataArrPtr+=4  == iDataArr++)

 

To get each Byte of iDataArr:

cDataArrPtr=&iDataArr[pos];

byte0=(uint8)*(cDataArrPtr+0);

byte1=(uint8)*(cDataArrPtr+1);

byte2=(uint8)*(cDataArrPtr+2);

byte3=(uint8)*(cDataArrPtr+3);

 

It's more  reasonable (for me) ;-)

 

Rgds,

Kai

View solution in original post

0 Kudos