Lazarus

Programming => General => Topic started by: BlueIcaro on October 17, 2020, 02:50:20 pm

Title: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
Post by: BlueIcaro 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
Title: Re: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
Post by: PascalDragon 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.
Title: Re: char model[MAX_GEVSTRING_LENGTH+1] convert to pascal?
Post by: BlueIcaro on October 17, 2020, 03:22:40 pm
Thanks PascalDragon!.
/BlueIcaro
TinyPortal © 2005-2018