(静的/固定メモリ割り当て)に混同していない&(動的なメモリ割り当て)のコンセプト:)
は、あなたのコンセプト仲間私は明確にしましょう。
次の質問に関連
、
C supports two type of array.
1. 静的配列は - "コンパイル時" のメモリを割り当てられています。
2. ダイナミックアレイ - は「実行時間」にメモリが割り当てられます。
Ques。配列が静的か動的かを調べる方法は?
Ans。 配列の宣言構文: -
は
int array_Name[size]; //size defines the size of block of memory for an array;
だから、ポイントに来て - >
ポイント1.サイズは、アレイへのコンパイル時に与えられた場合、それは「スタティックメモリ割り当て」です。サイズは決して変更されないので、固定サイズメモリ割り当てとも呼ばれます。 CでのARRAYの制限です。
ex。 int arr[10]; //10 is size of arr which is staticly defined int brr[] = {1000, 2, 37, 755, 3}; //size is equal to the no. of values initilizes with.
ポイント2.コンパイル時に配列にサイズが指定されている場合、動的メモリ割り当てです。 これは、stdlib.hで定義されているmalloc()関数によって実現されます。
は今、そのは、あなたのコードの明確化です: - は
#include <stdio.h>
int main(void) {
// your code goes here
int a = 2;
int b = 3;
int c;
c = a + b; //c is calculated at run time
int arr[c]; //Compilor awaiting for the value of c which is given at run time but,
arr[5] = 0; //here arr is allocated the size of 5 at static(compile) time which is never be change further whether it is compile time in next statements or run time.
printf("%d",arr[5]);
return 0;
}
ので、(サイズ5の)配列は、ARRで値0を保持している[5]。
他の配列インデックスにはまだガベージ値が表示されます。
あなたはこの問題を解決するには満足しているでしょう:)
この機能は過去1世紀に「新しい」追加されましたか? –
既に初期化していますか? printf( "%d"、arr [4])のようなものをやっていたならば。 –