如何在PL1编程语言中使用指针

发布于 2025-01-18 04:30:28 字数 500 浏览 2 评论 0原文

我正在尝试学习 PL1 中指针的使用。然而,以下方法不起作用

  DCL MYPTR PTR;                               
    DCL MYVAR CHAR(10) INIT('1234567890');       
    PUT SKIP(2) LIST('DISPLAY MYVAR: ',MYVAR);   
    MYPTR = ADDR(MYVAR);                             
    PUT SKIP(2) LIST('DISPLAY MYPTR: ',MYPTR);  

输出:

 DISPLAY MYVAR:          1234567890               
                                                 
DISPLAY MYPTR:                                   

I am trying to learn the use of pointers in PL1. However below approach is not working

  DCL MYPTR PTR;                               
    DCL MYVAR CHAR(10) INIT('1234567890');       
    PUT SKIP(2) LIST('DISPLAY MYVAR: ',MYVAR);   
    MYPTR = ADDR(MYVAR);                             
    PUT SKIP(2) LIST('DISPLAY MYPTR: ',MYPTR);  

OUTPUT:

 DISPLAY MYVAR:          1234567890               
                                                 
DISPLAY MYPTR:                                   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2025-01-25 04:30:28

指针是包含存储地址的变量。您可以使用它们来重新映射具有不同布局的存储区域。例如,假设单个数据集中有具有不同布局的记录,例如:

dcl 1 RecordType1,
      2 RecType        char( 01 ),
      2 Field01        char( 10 ),
      2 Field02        char( 20 ),
      2 Number01       decimal packed( 10,2 ),
      2 Field03        char( 10 );

dcl 1 RecordType2,
      2 RecType        char( 01 ),
      2 Field01        char( 05 ),
      2 *              char( 02 ), 
      2 Number01       bin fixed( 31 ),
      2 Numner02       bin fixed( 31 ),
      2 Field02        char( 100 );

这些声明预留两个不同的存储区域,每个类型一个。请注意,记录具有不同的长度。如果您只知道类型以及读取记录后的长度,您将如何读取记录?您需要执行以下操作:

  • 读入足够长的区域以容纳最长的类型,
  • 检查类型,
  • 移动到结构
  • 根据类型处理各个字段

。这涉及到许多不必要的数据移动。

使用指针以及声明中关联的 based() 属性,您可以将结构定义为映射,即无需底层存储。然后,您可以对所有映射使用单个指针。

dcl 1 RecordType1      based( pRecord ),
      2 RecType        char( 01 ),
      2 Field01        char( 10 ),
      2 Field02        char( 20 ),
      2 Number01       decimal packed( 10,2 ),
      2 Field03        char( 10 );

dcl 1 RecordType2      based( pRecord ),
      2 *              char( 01 ),
      2 Field01        char( 05 ),
      2 *              char( 02 ), 
      2 Number01       bin fixed( 31 ),
      2 Numner02       bin fixed( 31 ),
      2 Field02        char( 100 );

dcl pRecord            prt;
dcl LongestRecord      char( 116 );

pRecord = addr( LongestRecord );

现在,您执行如下操作:

  • 将记录读入 LongestRecord 字段,
  • 通过检查类型字段 RecType 来测试类型(假设类型指示器位于相同位置)每种类型)。
  • 通过限定变量引用访问各个文件,例如 RecordType1.Field01RecordType2.Number02

不再有不必​​要的数据从输入区域移动到映射区域。

如果您读取记录从数据集中,您甚至可以避免第一次移动并直接访问输入缓冲区中的记录;只需告诉读取语句设置指针,而不是将数据移至 LongestRecord 字段:

dcl fInput             external file record input;
...
read file( fInput ) set( pRecord );

您现在可以删除 LongestRecord 变量的声明,并将语句设置 < code>pRecord 到该变量的地址。

为了完整起见:PL/I 提供了另一种方法来映射具有两个或多个不同布局的存储区域:UNION,但这不是这里的问题。

Pointers are variables that contain a storage address. You use them to remap a storage area with different layout. For example, assume you've got records in a single data set that have different layout, say:

dcl 1 RecordType1,
      2 RecType        char( 01 ),
      2 Field01        char( 10 ),
      2 Field02        char( 20 ),
      2 Number01       decimal packed( 10,2 ),
      2 Field03        char( 10 );

dcl 1 RecordType2,
      2 RecType        char( 01 ),
      2 Field01        char( 05 ),
      2 *              char( 02 ), 
      2 Number01       bin fixed( 31 ),
      2 Numner02       bin fixed( 31 ),
      2 Field02        char( 100 );

These declarations set aside two distinct storage areas, one for each type. Note that the records have different lengths. How would you read in the records, if you only know the type, and with this the length after having read the record? You would need to do something like:

  • reading into an area long enough for the longest type,
  • check the type,
  • move to the structure according to the type
  • process the individual fields.

This involves a lot of unnecessary data moves.

Using pointers, and the associated based() attribute in declarations, you can define the structures as mapping, i.e. witout underlying storage. You then use a single pointer for all mappings.

dcl 1 RecordType1      based( pRecord ),
      2 RecType        char( 01 ),
      2 Field01        char( 10 ),
      2 Field02        char( 20 ),
      2 Number01       decimal packed( 10,2 ),
      2 Field03        char( 10 );

dcl 1 RecordType2      based( pRecord ),
      2 *              char( 01 ),
      2 Field01        char( 05 ),
      2 *              char( 02 ), 
      2 Number01       bin fixed( 31 ),
      2 Numner02       bin fixed( 31 ),
      2 Field02        char( 100 );

dcl pRecord            prt;
dcl LongestRecord      char( 116 );

pRecord = addr( LongestRecord );

Now, you do something like this:

  • read the record into the LongestRecord field,
  • test the type by inspecting the type field RecType (assuming the type indicator is at the same position for each type).
  • Access the individual files via qualified variable reference, e.g. RecordType1.Field01, or RecordType2.Number02

No more unnecessary data moves from input area to mapping area..

If you read the records from a data set, you can even avoid the first move and access the records directly in the input buffer; just tell the read statement to set the pointer, instead of moving the data into the LongestRecord field:

dcl fInput             external file record input;
...
read file( fInput ) set( pRecord );

You can now drop the declaration for the LongestRecord variable, and the statement setting pRecord to the address of that variable.

For completeness, only: PL/I offers another way to map a storage area with two or more different layouts: UNION, but this is not the question here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文