C言語のmalloc関数とは何ですか?
malloc()関数はメモリ割り当ての略で、メモリのブロックを動的に割り当てます。
指定されたサイズのメモリスペースを予約し、メモリ位置を指すnullポインタを返します。
malloc()関数はガベージ値を運びます。返されるポインタはvoid型です。
malloc()関数の構文は次のとおりです-
ptr = (castType*) malloc(size);
例
次の例は、malloc()関数の使用法を示しています。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(){
char *MemoryAlloc;
/* memory allocated dynamically */
MemoryAlloc = malloc( 15 * sizeof(char) );
if(MemoryAlloc== NULL ){
printf("Couldn't able to allocate requested memory\n");
}else{
strcpy( MemoryAlloc,"TutorialsPoint");
}
printf("Dynamically allocated memory content : %s\n", MemoryAlloc);
free(MemoryAlloc);
} 出力
上記のプログラムを実行すると、次の結果が得られます-
Dynamically allocated memory content: TutorialsPoint
-
C言語のstrcmp()関数とは何ですか?
Cライブラリ関数intstrcmp(const char * str1、const char * str2) str1が指す文字列を比較します str2が指す文字列へ 。 文字の配列は文字列と呼ばれます。 宣言 以下は配列の宣言です- char stringname [size]; 例-charstring[50];長さ50文字の文字列 初期化 単一文字定数の使用- char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,&ls
-
C言語のstrcpy()関数とは何ですか?
Cライブラリ関数char* strcpy(char * dest、const char * src) srcが指す文字列をコピーします 宛先へ 。 文字の配列は文字列と呼ばれます。 宣言 以下は配列の宣言です char stringname [size]; 例-charstring[50];長さ50文字の文字列 初期化 単一文字定数の使用- char string[10] = { ‘H’, ‘e’, ‘l’, ‘l’, ‘o’ ,‘\0’}