:: reverse dictionary ::
※ソースファイルについて
メモリ領域を割り当てるには、malloc 関数を使います。
#include <stdio.h> #include <stdlib.h>
int *mem; /* mem[0], mem[1], mem[2] を割り当てる */ mem = (int *)malloc(sizeof(int) * 3); if (mem == NULL) { printf("メモリ領域の割り当てに失敗しました。\n"); return EXIT_FAILURE; } else { mem[0] = 1; mem[1] = 2; mem[2] = 3; printf("mem[0]=%d\n", mem[0]); printf("mem[1]=%d\n", mem[1]); printf("mem[2]=%d\n", mem[2]); } free(mem);
割り当てに成功した場合は、次のような出力になります。
(mem に int のサイズ × 3 バイト分の領域が割り当てられます)
mem[0]=1 mem[1]=2 mem[2]=3
void *malloc(size_t sz)
sz バイトのメモリ領域を割り当てます。
sz … 割り当てるメモリ領域のバイト数
・割り当て成功 … 割り当てたメモリ領域へのポインタ
・割り当て失敗 … NULL
割り当てたメモリ領域は、必ず free 関数で解放する必要があります。
Copyright (C) 2005-2007 Noto Watabe. All rights reserved.
e-mail:wmh@always-pg.com