如何将带有char数组的C记录类型转换为Ada记录类型

时间:2018-08-13 06:14:42

标签: ada

我需要在源代码级别将几个C程序转换为Ada,即整个程序仅转换为Ada(不是从Ada调用C代码的混合编程)。

例如:

enum { MAXSZFNAME=15, MAXSZLNAME=23 }; 
typedef struct
   {
   char firstname [ MAXSZFNAME+1 ]; 
   char lasttname [ MAXSZLNAME+1 ]; 
   bool ismale;
   } PERSON;
PERSON candidate;
initialse candidate at run-time:
   strcpy ( candidate.firstname, "Fred" );
   strcpy ( candidate.lastname, "Brown" );
   candidate.ismale = true;
initialse candidate at compile-time:
PERSON candidate = { "Fred", "Brown", true };

我如何将上面的C代码示例转换为Ada?

4 个答案:

答案 0 :(得分:1)

对于已知的最大长度字符串,只需解决问题的一个问题,就可以使用Ada.Strings.Bounded包。

答案 1 :(得分:0)

您可以写:

enum { MAXSZFNAME=15, MAXSZLNAME=23 };

type Maximum_Name_Lengths is (Maximum_First_Name_Length, Maximum_Last_Name_Length);
for Maximum_Name_Lengths use (Maximum_First_Name_Length => 15,
                              Maximum_Last_Name_Length  => 23);

但是我觉得在您的实际问题中这没有任何意义。

答案 2 :(得分:0)

您声明这样的记录:

type Name_Of_Record_Type is
   record
      Name_Of_Field_1 : Type_Of_Field_1;
      Name_Of_Field_2 : Type_Of_Field_2 := Default_Initial_Value_For_Field_2;
      [...] 
   end record;

您声明这样的记录类型的常量:

Name_Of_Record_Constant : constant Name_Of_Record_Type
                            := (Name_Of_Field_1 => [some expression],
                                Name_Of_Field_2 => [some expression],
                                [...]);

您初始化这样的记录类型的变量:

Name_Of_Record_Variable : Name_Of_Record_Type
                            := (Name_Of_Field_1 => [some expression],
                                Name_Of_Field_2 => [some expression],
                                [...]);

您将这样分配给完整的记录:

Name_Of_Record_Variable := (Name_Of_Field_1 => Value_Of_Field_1,
                            Name_Of_Field_2 => Value_Of_Field_2,
                            [...]);

您将这样的记录分配给单个组件:

Name_Of_Record_Variable.Name_Of_Field_1 := [some_expression];

答案 3 :(得分:0)

Max_First_Name : constant := 15;
Max_Last_Name  : constant := 23;

type Person is record
   First_Name : String (1 .. Max_First_Name);
   Last_Name  : String (1 .. Max_Last_Name);
   Male       : Boolean;
end record;

Candidate : Person;

这似乎很简单,所以我不确定您遇到什么困难。