次のようなサイズの文字ポインタの配列を 1 つ作成するとします。
char* temp[10];
//need intialisation here..
temp[0] = "BLAH";
temp[1] = "BLAH";
temp[3] = "BLAH";
.
.
.
temp[9] = "BLAH";
//Need reinitialise..
temp[10] = "BLAH";
temp[11] = "BLAH";
どうすれば初期化できますか?
しばらくしてからサイズ 20 で再初期化する方法は?
malloc()
これを行うのにcalloc()
便利ですか?はいの場合、文字へのポインタの配列で使用する方法は?
[編集]
私のコードと要件、基本的にはCでファイルを読み取りたいが、1文字を無駄にすることはありません...テキストファイルからデータを読み取るコードは次のとおりです。
FILE *ptr_file;
/* Allocate space for ten strings */
/* Allocate space for ten strings */
char** list = (char **)malloc(10 * sizeof(char));
/* Reallocate so there's now space for 20 strings */
/* And initialize the new entries */
ptr_file =fopen(LogFileCharName,"rb");
if (!ptr_file)
return 1;
int __index = 0;
wchar_t CurrentString[1000];
while(fgetws (CurrentString , 1000 , ptr_file) != NULL)
{
char* errorDes;
errorDes = new char[1000];
wcstombs(errorDes, CurrentString, 1000);
list[__index] = errorDes;
if( __index>10)
{
(char**)realloc(list, 20 * sizeof(char *));
}
__index++;
}
サイズが10を超えた場合、サイズを変更する必要があります。このために、Microsoft Visual Studio の win32 コンソール アプリケーション タイプを使用しています。