开发者

how to assign the address pointed by a pointer to another local pointer

I'm doing a video processing project, and I got struck in assigning the block address for sending to the dct function.

The following line is not taking the correct assignment address as the right hand variable pointing to.

temp = (unsigned short *)((unsigned short *)(p_vqi->luma + j) + l);

so temp does not contain the correct开发者_如何学Go address pointed by the p_vqi->luma variable, where j and i will be incremented 16 times on each step for a maximum of 144 and 176 respectively.


The thing that often gets people with pointer math is that it doesn't add one byte at a time, it adds one sizeof(thing pointed to) at a time, so you're going to skip over j lumas, however big that is, then i unsigned shorts, however big that is on your architecture. Usually, it's easier and more portable when working with fixed formats to work straight in bytes, like:

uint8_t* temp = (uint8_t*)p_vqi->luma;
temp += j*16 + i;


Be aware that adding a number to a pointer increments the pointer by that number of elements, not bytes. In other words, you're first adding j * sizeof(the type of the luma entries) to the pointer, and then i * sizeof(unsigned short) which for most implementations are two bytes.

If however you want to add j + i bytes you should rather do something like this.

temp = (unsigned short *)((intptr_t)p_vqi->luma + j + i);

That should give you a pointer to an unsigned short that is advanced i + j bytes from the original. The intptr_t type is C99, if you need to be compatible with older compilers use unsigned long instead.


Addition (& substraction) of pointers have a different behavior that the standard arithmetic operations. These operations will be factorised by the size of the data they represent.

Lets take an example, considering that char is one byte, short is two bytes and an int is 4 bytes length:

char * p1;
shot * p2;
int * p3;

p1 += 1; // p1 will be incremented by 1
p2 += 1; // p2 will be incremented by 2
p3 += 1; // p3 will be incremented by 4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜