how can I move a C pointer +1

Bare metal programming in PureBasic, for experienced users
Wolfram
Enthusiast
Enthusiast
Posts: 603
Joined: Thu May 30, 2013 4:39 pm

how can I move a C pointer +1

Post 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)
macOS Catalina 10.15.7
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: how can I move a C pointer +1

Post 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)


Wolfram
Enthusiast
Enthusiast
Posts: 603
Joined: Thu May 30, 2013 4:39 pm

Re: how can I move a C pointer +1

Post 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)
macOS Catalina 10.15.7
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: how can I move a C pointer +1

Post 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. :)
Dawn will come inevitably.
User avatar
mk-soft
Always Here
Always Here
Posts: 6201
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: how can I move a C pointer +1

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Wolfram
Enthusiast
Enthusiast
Posts: 603
Joined: Thu May 30, 2013 4:39 pm

Re: how can I move a C pointer +1

Post by Wolfram »

Thanks MK!

Where did you get the information that you have to use f_ in this case?
macOS Catalina 10.15.7
User avatar
idle
Always Here
Always Here
Posts: 5834
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: how can I move a C pointer +1

Post by idle »

In c structs pb uses f_ to denote the fields.
User avatar
useful
Enthusiast
Enthusiast
Posts: 402
Joined: Fri Jul 19, 2013 7:36 am

Re: how can I move a C pointer +1

Post 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.
Dawn will come inevitably.
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: how can I move a C pointer +1

Post 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

Post Reply