2

「配列」内に格納されている整数を出力したい場合は、これは実際にはメモリ内のポインタです。コードを使用する場合:

int main(void)
{
  int *arr = malloc(3*sizeof(int));
  int *p = arr;
  *arr++=1;
  *arr++=2;
  *arr=3;

  while (??)           // what should be filled in the while?
    printf("%d",*p);   // so that we can get the validly stored elements?

  return 0;
}
4

1 に答える 1

5

C には、動的に割り当てられたメモリのチャンクのサイズを検出する組み込みの方法がありません。サイズを保存し、「横に」渡す必要があります。

これに対処する 1 つの方法はstruct、ポインターとsize_t、配列に割り当てられた要素の数を表す値を組み合わせて作成することです。

于 2012-04-07T20:31:58.440 に答える