Recent

Author Topic: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?  (Read 182 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 616
char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
« on: October 17, 2020, 02:50:20 pm »
Hello, I want to translate this C sentence:
Code: [Select]
char   model[MAX_GEVSTRING_LENGTH+1];
But I'm no sure is can be converted like this:
Code: [Select]
  model[MAX_GEVSTRING_LENGTH+1] : string;
or should be converted to a array of char?
Code: [Select]
model: array [0..MAX_GEVSTRING_LENGTH + 1] of char;

What's the correct ?

Thanks

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

PascalDragon

  • Hero Member
  • *****
  • Posts: 2397
  • Compiler Developer
Re: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
« Reply #1 on: October 17, 2020, 02:59:05 pm »
Hello, I want to translate this C sentence:
Code: [Select]
char   model[MAX_GEVSTRING_LENGTH+1];
But I'm no sure is can be converted like this:
Code: [Select]
  model[MAX_GEVSTRING_LENGTH+1] : string;
or should be converted to a array of char?
Code: [Select]
model: array [0..MAX_GEVSTRING_LENGTH + 1] of char;

What's the correct ?

In C TYPE NAME[COUNT] defines an array of COUNT elements of type TYPE, thus COUNT - 1 is the highest index. Thus the correct translation for Pascal is the following:

Code: Pascal  [Select][+][-]
  1. model: array[0..MAX_GEVSTRING_LENGTH] of Char;

Note the missing + 1, because Pascal uses the lowest and highest index instead of the count of elements and as we've seen COUNT - 1 = (MAX_GEVSTRING_LENGTH + 1) - 1 = MAX_GEVSTRING_LENGTH is the highest index.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 616
Re: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
« Reply #2 on: October 17, 2020, 03:22:40 pm »
Thanks PascalDragon!.
/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

 

TinyPortal © 2005-2018