Recent

Author Topic: Error trying to initialize a char variable  (Read 790 times)

tfurnivall

  • New Member
  • *
  • Posts: 45
Error trying to initialize a char variable
« on: May 19, 2025, 09:40:45 pm »
My jaw hit the ground when I got this error message out of what I thought was pretty standard Pascal:

Code: Pascal  [Select][+][-]
  1. var
  2.  
  3. ch : char;

and then in the actual code area:

Code: Pascal  [Select][+][-]
  1. ch := '';

I get an error "Got Constant String expected Char

When I first encountered Pascal, back in the 70s, I learnt that the way to initialize a char variable to 'nothing' was to use the form I gave above, assigning it to an empty string constant.
The guidance (from Free Pascal and Lazarus {translated from the Russian}) is:

The values of character variables and constants must be enclosed in single
quotation marks, such as: 'a', 'b', '+', in source code.

So, faced with a grumpy compiler, I went back and defined a new variable:
Code: Pascal  [Select][+][-]
  1. st : string;

at which point
Code: Pascal  [Select][+][-]
  1. st := '';
worked just fine.

Sooooo..

if a String is really a Packed array of charcters and char is a single character, and we can index into a string and bring out a character, then why can't we just assign the '' value to the char variable?

I could construct an empty string (st := ''; ) but then it wouldn't have any characters, so I can't get at them via the indexing method. The same goes for adding any number of empty strings - they don't exist so I can't access them. I get that.

If you have a character variable that you want to initialize, how do you do it?

Confused, cranky and curmudgeonly,

Tony



cdbc

  • Hero Member
  • *****
  • Posts: 2217
    • http://www.cdbc.dk
Re: Error trying to initialize a char variable
« Reply #1 on: May 19, 2025, 09:55:05 pm »
Hi
Code: Pascal  [Select][+][-]
  1. Ch:= #0; // or
  2. Ch:= ' '; {space NOT empty}
A char cannot be empty like a string, but #0 is damn close...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

440bx

  • Hero Member
  • *****
  • Posts: 5477
Re: Error trying to initialize a char variable
« Reply #2 on: May 19, 2025, 09:55:32 pm »
It all sounds very reasonable but think of it this way...

if instead of a char variable you had an integer variable, would you expect it possible to "initialize" the integer variable with an "empty" integer ?

Probably not, because there is no such thing as an "empty" integer.  It's the same with a char variable, there is no such thing as an "empty" character.

However, there is such a thing as an empty string.  That's why assigning '' to a string works (as it should) and assigning '' to a char does _not_ work (as it should too.)

In the case of a string assigning it the value '', depending on the type of string (longstring or shortstring) causes the value assigned to be nil (in the case of longstring) or #0 in the case of shortstring.  IOW, a value is being assigned, the "empty string" isn't an "empty" variable.

HTH.
(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v4.0rc3) on Windows 7 SP1 64bit.

Fibonacci

  • Hero Member
  • *****
  • Posts: 755
  • Internal Error Hunter
Re: Error trying to initialize a char variable
« Reply #3 on: May 19, 2025, 10:13:29 pm »
String is not a simple type, its a structure (record). Its never "empty", you just set one of the fields (length) to 0. String is actually a pointer to this structure. Char on the other hand is a simple type and you access it directly, the value must have the size of the type, which is 1 byte.

Khrys

  • Full Member
  • ***
  • Posts: 243
Re: Error trying to initialize a char variable
« Reply #4 on: May 20, 2025, 07:19:01 am »
I think part of the confusion stems from the fact that both character and string literals use the same delimiters. Consider the following:

Code: Pascal  [Select][+][-]
  1. Foo: Char = 'A';
  2. Bar: String = 'B';
  3. //            ^
  4. // single quotes in Pascal

Code: C  [Select][+][-]
  1. char foo = 'a';
  2. char *bar = "B";
  3. //          ^
  4. // double quotes in C

With that being said, there are a few other ways of representing single characters. Here are all the representations of  'Hello' + LineEnding + 'World!'  I can think of (where  LineEnding  is  \r\n  aka CRLF, common on Windows):

Code: Pascal  [Select][+][-]
  1. A = 'Hello'#13#10'World!';       // Decimal char codes
  2. B = 'Hello'#$D#$A'World!';       // Hexadecimal char codes
  3. C = 'Hello'#&15#&12'World!';     // Octal char codes
  4. D = 'Hello'#%1101#%1010'World!'; // Binary char codes
  5. E = 'Hello'^M^J'World!';         // Control codes (e.g. git diff uses this format)

Thaddy

  • Hero Member
  • *****
  • Posts: 17198
  • Ceterum censeo Trump esse delendam
Re: Error trying to initialize a char variable
« Reply #5 on: May 20, 2025, 09:04:28 am »
Benny is right: a (Ansi) char can not be empty, must lie between #0 and #255.
#0 is best.
« Last Edit: May 20, 2025, 10:49:46 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Zvoni

  • Hero Member
  • *****
  • Posts: 2982
Re: Error trying to initialize a char variable
« Reply #6 on: May 20, 2025, 12:28:57 pm »
Benny is right: a (Ansi) char can not be empty, must lie between #0 and #255.
#0 is best.
Which in Layman's terms means: Char is basically a TypeDef for a Byte
https://www.freepascal.org/docs-html/rtl/system/char.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 17198
  • Ceterum censeo Trump esse delendam
Re: Error trying to initialize a char variable
« Reply #7 on: May 20, 2025, 01:17:25 pm »
No, not exactly, only when you mimic a C style char *. But it is a fair comparison never the less.

Pascal char and byte are distinct types as opposed to C.
« Last Edit: May 20, 2025, 01:19:59 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1530
    • Lebeau Software
Re: Error trying to initialize a char variable
« Reply #8 on: May 20, 2025, 10:09:51 pm »
String is not a simple type, its a structure (record). Its never "empty", you just set one of the fields (length) to 0. String is actually a pointer to this structure.

Mostly true, however an "empty" String actually uses a nil pointer, so there is no record stored. Thus, the record's length field is never 0.  When you call Length() on a String, it returns 0 if the pointer is nil, otherwise it returns the record's length field.

Char on the other hand is a simple type and you access it directly, the value must have the size of the type, which is 1 byte.

Or 2 bytes under {$MODESWITCH UNICODESTRINGS}/{$MODE DelphiUnicode}.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

silvercoder70

  • Full Member
  • ***
  • Posts: 190
    • Tim Coates
Re: Error trying to initialize a char variable
« Reply #9 on: May 21, 2025, 02:30:07 pm »
just to add to the replies...

- consider char and integer as simple types then neither of these have no value.
- a char variable always holds a value — even if you don't explicitly set it, it will still contain some default value (often whatever is in memory at that location unless it's initialized)

lastly, strings are a bit more than packed arrays (and is a  managed type). I don't know the page number(s), but if you have a look at the Object Pascal book by Marco Canto (spelling?) you can read all about it there.
🔥 Pascal Isn’t Dead -> See What It Can Do: @silvercoder70 on YouTube

 

TinyPortal © 2005-2018