本文に進む 日本−日本語
日本HPホーム 製品とサービス お客様サポート/ ダウンロード ソリューション ご購入の方法
≫ お問い合わせ
詳細検索オプション
日本HPホーム
HP C/HP-UX リリースノート B.11.11.04: HP 9000 コンピュータ > 第1章 新機能および変更された機能

可変長配列

≫ 

テクニカル ドキュメント

PDF版
フィードバック
ここから本文が始まります

 ≫ 目次

このリリースでは、可変長配列(VLA)機能を提供します。VLA は C99 標準に含まれています(ISO/IEC 9899:1999)。VLA では、配列宣言子の[] で囲まれた整数式として、変数式または * を使用することができます。宣言にこのような配列宣言子が含まれている識別子は、可変修飾(VM)型と呼ばれます。

VM 型を持つすべての識別子は、次のいずれかでなければなりません。

  • 配列宣言子の中の式が変数式である場合は、ブロック範囲または関数プロトタイプ範囲

  • 式が * である場合には、関数プロトタイプ範囲

注記: VLA は ANSI 拡張モード(-Ae)でのみサポートされています。

static または extern 記憶領域クラス指定子で宣言された配列は VM 型を持つことができません。ただし、static 記憶領域クラス指定子で宣言された配列へのポインタは VM 型を持つことができます。VM 型を持つすべての識別子は通常の識別子であり、構造体または共用体のメンバーになることはできません。

extern int n;

int A[n];            // Error - file scope VM type
extern int (*p) [n]; // Error - file scope VM type
int B[100];          // OK - file scope but not VM type
void foo(int m, int C[m] // OK - function prototype scope VM<
                         // type
{
  typedef int VLA[m] [m];  // OK - block scope VM type
  int D[m];                // OK - block scope with VM type
  static int E[m];         // Error - static specifier in VM type
  extern int F[m];         // Error - extern specifier in VM type
  int (*q)[m];            // OK - block scope with VM type
  extern int (*r)[m];      // Error - extern specifier in VM type
  static int (*s)[m] = &B; // OK - static specifier allowed in VM
                           // type since 's' is pointer to array
struct tag {
  int (*x)[n]; // Error - x not ordinary identifier
  int y[n];    // Error - y not ordinary identifier
  };
}

goto 文は、VM 型を持つ識別子の宣言の後ろにジャンプすることはできません。範囲内でのジャンプは可能です。

goto L1; // Error- going INTO scope of VM type
{
     int a[n];
     a[j] = 4;
L1:
     a[j] = 3;
     goto L2; // OK, - going WITHIN scope of VM type
     a[j] = 5;
L2:
     a[j] = 6;
}
goto L2; // Error- going INTO scope of VM type

VM 型を持つオブジェクトのサイズは、オブジェクトの宣言の時点で決定および固定され、後で変更することはできません。

次の例は、(int) のサイズを 4 と仮定しています。

 int n = 10, vla[n];
 n = 20;
 printf(""%d", sizeof(vla); // print 40, not 80
印刷用画面へ
プライバシー 本サイト利用時の合意事項
© Hewlett-Packard Development Company, L.P.