Recent

Author Topic: Can this be done in FPC ?  (Read 3984 times)

440bx

  • Hero Member
  • *****
  • Posts: 1085
Can this be done in FPC ?
« on: November 25, 2018, 01:28:26 am »
Hello,

In C/C++ I can declare a pointer to point inside a null terminated character string as shown below:

Code: Pascal  [Select]
  1. const char* pchar = "some character string";
  2.  
  3. const char* otherpchar = pchar + 5;  // points to "character"
  4.  

Can this be done in FPC ?  (important: the value of otherpchar must be determined at compile time not runtime.)

Thank you for your help.

using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

creaothceann

  • Full Member
  • ***
  • Posts: 109
Re: Can this be done in FPC ?
« Reply #1 on: November 25, 2018, 02:18:08 am »
Like this?

Code: Pascal  [Select]
  1. program StringPtrTest;
  2. uses
  3.         SysUtils;
  4.  
  5.  
  6. const
  7.         Tab    = #9;
  8.         Text_s = 'Test';
  9.  
  10.  
  11. var
  12.         s : array[1..Length(Text_s)] of AnsiChar = Text_s;
  13.         p : ^AnsiChar = @s[1];
  14.         i : PtrUInt absolute p;
  15.  
  16.  
  17. begin
  18. WriteLn('$' + IntToHex(i, SizeOf(i) * 2) + Tab + p^);  Inc(i);
  19. WriteLn('$' + IntToHex(i, SizeOf(i) * 2) + Tab + p^);  Inc(i);
  20. WriteLn('$' + IntToHex(i, SizeOf(i) * 2) + Tab + p^);  Inc(i);
  21. WriteLn('$' + IntToHex(i, SizeOf(i) * 2) + Tab + p^);
  22. ReadLn;
  23. end.
« Last Edit: November 25, 2018, 02:20:08 am by creaothceann »

440bx

  • Hero Member
  • *****
  • Posts: 1085
Re: Can this be done in FPC ?
« Reply #2 on: November 25, 2018, 02:51:46 am »
Like this?
Yes!  :)  that does what I want. 

Thank you!
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

lucamar

  • Hero Member
  • *****
  • Posts: 1982
Re: Can this be done in FPC ?
« Reply #3 on: November 25, 2018, 03:20:52 am »
You don't even need variable i

Code: Pascal  [Select]
  1. program project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses sysutils;
  5.  
  6. const
  7.   Tab    = #9;
  8.   Text_s = 'Some character string';
  9.  
  10. var
  11.   s : array[1..Length(Text_s)] of AnsiChar = Text_s;
  12.   p : ^AnsiChar = @s[1];
  13.   {i : PtrUInt absolute p;}
  14.   x: Integer;
  15.  
  16. begin
  17.   for x := 1 to Length(s) - 1 do begin
  18.     WriteLn('$' + IntToHex(PtrUInt(p), SizeOf(p) * 2) + Tab + p^);
  19.     Inc(p);
  20.   end;
  21.   WriteLn('$' + IntToHex(PtrUInt(p), SizeOf(p) * 2) + Tab + p^);
  22.   ReadLn;
  23. end.

Drawback is that without it the typecast gives a "conversion not portable" hint but that is a non-issue in this case.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.2/2.0.4  - FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

howardpc

  • Hero Member
  • *****
  • Posts: 3096
Re: Can this be done in FPC ?
« Reply #4 on: November 25, 2018, 09:20:50 am »
The program can be simplified to this:
Code: Pascal  [Select]
  1. program StringPtrTest;
  2.  
  3. const
  4.   Text_s = 'Test';
  5.  
  6. var
  7.   p, pEnd: PChar;
  8.  
  9. begin
  10.   p := PChar(Text_s);
  11.   pEnd := p + Length(Text_s);
  12.   while p < pEnd do begin
  13.     WriteLn('$', PtrUInt(p), #9, p^);
  14.     Inc(p);
  15.   end;
  16.   ReadLn;
  17. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 8671
Re: Can this be done in FPC ?
« Reply #5 on: November 25, 2018, 11:14:57 am »
Well close
This is closer:
Code: Pascal  [Select]
  1. var
  2.   s1:array[0..20] of char = 'some character string';
  3.   s2:array[0..15] of char absolute s1[5];  
  4. begin
  5.   writeln(s1);
  6.   writeln(s2); 
  7. end.

Since C doesn't know strings: they are arrays of char.
Basically anything that can be expressed in K&R C can be expressed in Pascal too.
Also note the original code is unsafe (you can over-index it..) and this Pascal equivalent isn't.... 8-)
Furthermore: that original code declares the equivalent of Pascal vars: not const, but const *

Oh, and absolute means it is known at compile time.

This will demonstrate that:
Code: Pascal  [Select]
  1. var
  2.   s1:array[0..20] of char = 'some character string';
  3.   s2:array[0..length(s1)-5] of char absolute s1[5];  
  4. begin
  5.   writeln(s1);
  6.   writeln(s2); 
  7. end.

« Last Edit: November 25, 2018, 11:43:59 am by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1085
Re: Can this be done in FPC ?
« Reply #6 on: November 25, 2018, 11:54:34 am »
Well close
This is closer:
Code: Pascal  [Select]
  1. var
  2.   s1:array[0..20] of char = 'some character string';
  3.   s2:array[0..15] of char absolute s1[5];  
  4. begin
  5.   writeln(s1);
  6.   writeln(s2); 
  7. end.

it's more succinct to simply write
Code: Pascal  [Select]
  1. s2 : pchar = @s1[5];
and in addition to being more succinct, it also works in a const block whereas "absolute" does not.

Claiming that C knows arrays is a bit of a stretch.  It knows pointers and pointer arithmetic.  Any pointer (as long as it isn't void) is an array.

Quote
Basically anything that can be expressed in K&R C can be expressed in Pascal too.
that might be true.  I don't remember K&R C well enough to argue that point BUT, FPC cannot express all that can be expressed in C++.

Quote
Also note the original code is unsafe (you can over-index it..) and this Pascal equivalent isn't....
That pascal equivalent is equivalently unsafe.  You can set the index in "absolute s1" to way past the boundaries of s1. That said, credit must be given to FPC for issuing a warning about it but, it won't stop you from doing it (which is a good thing.)

Quote
Furthermore: that original code declares vars: not const, but const *
there is no way to declare a const * in FPC.  i.e, FPC cannot make a difference between a pointer to an constant array of characters and a zero based array of char in memory. 

In some situations (very few), C++ has stronger type checking than Pascal.  It is normally the opposite.











using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 8671
Re: Can this be done in FPC ?
« Reply #7 on: November 25, 2018, 12:19:07 pm »
The whole of the C language as envisioned by K&R is that everything is an array. C++ is a different story: Bjarne Soustrup wanted to re-invent object Pascal...with curly brackets... So everything is basically a comment... :D
Note you can over complicate anything in any language... I simply showed that pointer dereferences are not needed. Lets save @ for emails...
« Last Edit: November 25, 2018, 12:21:38 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

Thaddy

  • Hero Member
  • *****
  • Posts: 8671
Re: Can this be done in FPC ?
« Reply #8 on: November 25, 2018, 12:24:21 pm »
Quote
Quote
Furthermore: that original code declares vars: not const, but const *
there is no way to declare a const * in FPC.  i.e, FPC cannot make a difference between a pointer to an constant array of characters and a zero based array of char in memory. 
Do you really understand what I wrote?
Pascal can do all of that. It is more a lack of knowledge from C programmers that always confuse some syntax with different meanings. Deliberately.
What I wrote is true, it is not a meaning but a fact.... (the example code). Meanings can differ, facts don't.
« Last Edit: November 25, 2018, 12:27:08 pm by Thaddy »
Most people that want to use threading should learn to patch their jeans first: use a needle.

440bx

  • Hero Member
  • *****
  • Posts: 1085
Re: Can this be done in FPC ?
« Reply #9 on: November 25, 2018, 01:59:08 pm »
What I wrote is true, it is not a meaning but a fact.... (the example code). Meanings can differ, facts don't.
There is a very obvious fact in most of what you write and, that is, you're a legend in your own mind.  It's a start.

ETA:

You're making progress... you didn't get grumpy this time.  There might be hope for you.  (yes, I know, I'm an optimist.)
« Last Edit: November 25, 2018, 09:37:54 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Windsurfer

  • Sr. Member
  • ****
  • Posts: 362
    • Windsurfer
Re: Can this be done in FPC ?
« Reply #10 on: November 25, 2018, 02:07:33 pm »
This is off topic, but I have not used pointers in fpc/lazarus LCL for many years.

I know there are plenty of pointers in fpc and LCL, but how many places are there left where pointers are necessary for normal high level programming?

This might merit a page in the wiki that C programmers could be directed to.

Blaazen

  • Hero Member
  • *****
  • Posts: 2782
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can this be done in FPC ?
« Reply #11 on: November 25, 2018, 02:43:49 pm »
@ Windsurfer

Until you need some bindings to C library. This is from PortAudio:  :)
Code: Pascal  [Select]
  1.   PaStream = Pointer;
  2.   PPaStream = ^PaStream;
  3.   PPPaStream = ^PPaStream;
Lazarus 2.1.0 r59757M FPC 3.3.1 r40507 x86_64-linux-qt Chakra, Qt 4.8.7/5.11.2, Plasma 5.14.2
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 7353
Re: Can this be done in FPC ?
« Reply #12 on: November 25, 2018, 02:49:16 pm »
I know there are plenty of pointers in fpc and LCL, but how many places are there left where pointers are necessary for normal high level programming?

True, but how often do you do purely high level programming? There is always  a bit of foreign language interfacing, decoding wire protocols (walking through a buffer), image manipulation, or something that just needs an extra bit of speed.

Also inside the frameworks that you use, there is always some of this code, and you need to be able to read and fix it if possible.

This is why I use a language that scales from low level to high level, instead of a purely ivory tower high level language.

As for this thread, pointers are largely the same in principle, details can vary though (like pointers into literals, pointer type equivalence etc).

440bx

  • Hero Member
  • *****
  • Posts: 1085
Re: Can this be done in FPC ?
« Reply #13 on: November 25, 2018, 08:28:09 pm »
I know there are plenty of pointers in fpc and LCL, but how many places are there left where pointers are necessary for normal high level programming?
More than many "new" programmers realize.  Every class declaration is a pointer declaration.  Its instantiation is what sets the pointer value.  The fact that the compiler is "hiding" the pointer from the programmer by doing automatic dereferencing doesn't eliminate it.

Object oriented programming is essentially a programming method that makes a level of pointer manipulation "accessible" to the "masses" through syntactic sugar.   

« Last Edit: November 25, 2018, 09:26:10 pm by 440bx »
using FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

howardpc

  • Hero Member
  • *****
  • Posts: 3096
Re: Can this be done in FPC ?
« Reply #14 on: November 25, 2018, 08:34:23 pm »
Object oriented programming is essentially a programming method that makes a level of pointer manipulation "accessible" to the "masses" through syntactic sugar.
Ditto for several other declarations: strings (not shortstrings, of course), dynamic arrays, interfaces...