Page 1 of 1

how can I move a C pointer +1

Posted: Mon Oct 14, 2024 8:16 pm
by Wolfram
Why does p2 return the same value as p ?

Code: Select all

x.l = $AABBCCDD
y.l


! unsigned short *p_p;
! p_p = &v_x ;
! v_y = *p_p;
Debug Hex(y)

! unsigned short *p_p2;
! p_p2 = (&v_x + 1);
! v_y = *p_p2;
Debug Hex(y)

Re: how can I move a C pointer +1

Posted: Mon Oct 14, 2024 10:22 pm
by idle

Code: Select all

x.l = $AABBCCDD
y.l

!typedef struct uint16 {
!unsigned short uint;
!} uint16;

! uint16 *p_p1 = (void*)((integer)(&v_x));
! v_y = p_p1->uint;
Debug Hex(y)

! uint16 *p_p2 = (void*)(((integer)(&v_x))+2);
! v_y = p_p2->uint;
Debug Hex(y)



Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 8:42 am
by Wolfram
is it also possible if I define the pointer in PB?

Code: Select all

;... like this not working example
*ptr.unicode = @x
! v_y = p_ptr->u;
Debug Hex(y)

Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 9:26 am
by useful
Wolfram wrote: Wed Oct 16, 2024 8:42 am

Code: Select all

...
*ptr.unicode = @x
...
Unfortunately, pointers in Purebasic do not have type.

They are always void

Therefore, additional effort is needed in the form of functions: peek[type], poke[type]
https://www.purebasic.com/documentation ... index.html

p.s. Sometimes purebasic behaves like basic. :)

Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 9:56 am
by mk-soft
useful wrote: Wed Oct 16, 2024 9:26 am
Wolfram wrote: Wed Oct 16, 2024 8:42 am

Code: Select all

...
*ptr.unicode = @x
...
Unfortunately, pointers in Purebasic do not have type.

They are always void

Therefore, additional effort is needed in the form of functions: peek[type], poke[type]
https://www.purebasic.com/documentation ... index.html

p.s. Sometimes purebasic behaves like basic. :)
Of course, pointers in PureBasic can have a structure type.
For simple types, these are predefined in PB. *ptr.integer, long, word, unicode, etc.
The access is also easier and faster than with the function calls of Peek and Poke.

Code: Select all

Define x.s = "01234"
Define y
Define *ptr.unicode = @x
;y = *ptr\u
! v_y = p_ptr->f_u;
Debug Hex(y)

Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 10:50 am
by Wolfram
Thanks MK!

Where did you get the information that you have to use f_ in this case?

Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 11:32 am
by idle
In c structs pb uses f_ to denote the fields.

Re: how can I move a C pointer +1

Posted: Wed Oct 16, 2024 12:44 pm
by useful
mk-soft wrote: Wed Oct 16, 2024 9:56 am
useful wrote: Wed Oct 16, 2024 9:26 am
Wolfram wrote: Wed Oct 16, 2024 8:42 am

Code: Select all

...
*ptr.unicode = @x
...
Unfortunately, pointers in Purebasic do not have type.

They are always void

Therefore, additional effort is needed in the form of functions: peek[type], poke[type]
https://www.purebasic.com/documentation ... index.html

p.s. Sometimes purebasic behaves like basic. :)
Of course, pointers in PureBasic can have a structure type.
For simple types, these are predefined in PB. *ptr.integer, long, word, unicode, etc.
The access is also easier and faster than with the function calls of Peek and Poke.

Code: Select all

Define x.s = "01234"
Define y
Define *ptr.unicode = @x
;y = *ptr\u
! v_y = p_ptr->f_u;
Debug Hex(y)
But I will hope that instead of the non-obvious way of typing through single-element structures, we will get just the pointer type and, as a result, pointer arithmetic.

Re: how can I move a C pointer +1

Posted: Tue Oct 22, 2024 10:14 am
by breeze4me
Without a structure.

Code: Select all

x.l = $AABBCCDD
y.l

! unsigned short *p_p;
! p_p = &v_x ;
! v_y = *p_p;
Debug Hex(y)    ;CCDD

! unsigned short *p_p2;

! p_p2 = (integer)(&v_x) + 1;
! v_y = *p_p2;
Debug Hex(y)    ;BBCC

! p_p2 = (integer)(p_p2) + 1;
! v_y = *p_p2;
Debug Hex(y)    ;AABB

! p_p2 = (integer)(&v_x) + 2;
! v_y = *p_p2;
Debug Hex(y)    ;AABB

! p_p2 = &v_x;
! p_p2 += 1;
! v_y = *p_p2;
Debug Hex(y)    ;AABB