0

In my case below I don't want c to be assigned to anything until the first character of the file is read.

I tried setting the Char variable c to nil (c := nil;) but compilation fails. I tried an empty string like below, and still doesn't work.

It works when I set it to an empty space, but it seems peculiar that I have to do that.

Is there any way to initialize a Char to a null like value as you can do in other languages?

program CSVToMarkdown;
 
{$mode objfpc}{$H+}{$J-}

uses
    Sysutils;

var
    f: File of Char;
    c: Char;

begin
    Assign(f, 'test.csv');
    Reset(f);
    c := '';

    while not Eof(f) do
    begin
        Read(f, c);
        Write(c);
    end;

     Close(f);

    ReadLn;
end.
BugHunterUK
  • 8,346
  • 16
  • 65
  • 121
  • Related: https://stackoverflow.com/questions/9618587/how-to-define-empty-char-in-delphi. You probably already know this, but of course you can set `c` to the ASCII NUL character: `c := #0`. In fact, you don't need to do this manually, because being a *global* variable, `c` is initialised to this. – Andreas Rejbrand Nov 15 '20 at 23:06
  • You don't need to initialize the variable *c* since it's assigned to (by *Read*) before it's read (by Write). – August Karlstrom Nov 16 '20 at 09:22

1 Answers1

2

NIL is a value for pointers, or reference types (interfaces,class, dyn arrays) in general.

Non reference types don't have a NIL value, the type char can take values from #0 to #255, and all are valid, though sometimes when interfacing to other languages #0 is interpreted as end of string.

If you mean nullable types like in Java or .NET, there is no default support for them as they have the disadvantage of the type becoming larger than need be (iow becoming a pseudo record with added NULL boolean).

There are some generics based solutions that try to implement nullable types, but I haven't used them, and they are not part of the standard distribution.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • I've edited my question. I accidentally put `initialize a string to a null`, what I meant was `initialize a Char to a null` – BugHunterUK Nov 15 '20 at 22:43
  • @BugHunterUK: Do you mean the ASCII NUL character? If so, it is `#0`, but `c` already has that value when the program starts, since it is a *global* variable. – Andreas Rejbrand Nov 15 '20 at 23:08
  • I'm pretty sure I was getting a compiler error about `c` not being initialized. I retried by removing `c := ' ';` and it worked. Problem solved. – BugHunterUK Nov 16 '20 at 01:44
  • A space character doesn't really seem the same as setting to a nil or null value. It seems like you might be using trial and error here. – David Heffernan Nov 16 '20 at 07:58
  • @DavidHeffernan pretty much. I've just started to learn the language. I was following an example from pascal docs. I wasn't aware it was already initialized to `#0`. – BugHunterUK Nov 16 '20 at 13:22
  • BugHunteruk: no it is not default initialized, but all on most targets global variables are NILled on startup. It is considered bad form to rely on it, since changing the code to a procedure (and thus local variables) would lead to problems – Marco van de Voort Nov 16 '20 at 14:49