C sizeof演算子 新しいページはコチラ
提供: yonewiki
(→sizeof演算子) |
(→sizeof演算子) |
||
13行: | 13行: | ||
sizeof演算子の基本的な使い方を以下に示します。 | sizeof演算子の基本的な使い方を以下に示します。 | ||
+ | <syntaxhighlight lang="cpp" line start="1"> | ||
+ | #pragma once | ||
+ | #include <iostream> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | struct stDate{ | ||
+ | int year; | ||
+ | int month; | ||
+ | int day; | ||
+ | }; | ||
+ | |||
+ | int _tmain(int argc, _TCHAR* argv[]){ | ||
+ | int pnTestArr[5] = {0,10,20,30,40}; | ||
+ | char pcTestArr[10] = "Size"; | ||
+ | printf("sizeof演算子\n"); | ||
+ | printf("stDate構造体(int型*3個) = %d[Byte]\n",sizeof(stDate)); | ||
+ | printf("sizeof演算子戻り値のサイズ = %d[Byte]\n",sizeof(sizeof(stDate))); | ||
+ | printf("\n"); | ||
+ | printf("int型 = %d[Byte]\n",sizeof(int)); | ||
+ | printf("short型 = %d[Byte]\n",sizeof(short)); | ||
+ | printf("char型 = %d[Byte]\n",sizeof(char)); | ||
+ | printf("long型 = %d[Byte]\n",sizeof(long)); | ||
+ | printf("double型 = %d[Byte]\n",sizeof(double)); | ||
+ | printf("float型 = %d[Byte]\n",sizeof(float)); | ||
+ | printf("bool型 = %d[Byte]\n",sizeof(bool)); | ||
+ | printf("size_t型 = %d[Byte]\n",sizeof(size_t)); | ||
+ | printf("\n"); | ||
+ | printf("ポインタ(32bitアプリの場合) = %d[Byte]\n",sizeof(stDate*)); | ||
+ | printf("int型静的配列 要素5 = %d[Byte]\n",sizeof(pnTestArr)); | ||
+ | printf("char型静的配列 要素10\n"); | ||
+ | printf("pcTestArr = %d[Byte]\n",sizeof(pcTestArr)); | ||
+ | printf(" (pcTestArr) + 0) = %d[Byte]\n",sizeof((pcTestArr) + 0)); | ||
+ | printf("*(pcTestArr) + 0) = %d[Byte]\n",sizeof(*(pcTestArr) + 0)); | ||
+ | printf("pcTestArr[0] = %d[Byte]\n",sizeof(pcTestArr[0])); | ||
+ | printf("pcTestArr[5] = %d[Byte]\n",sizeof(pcTestArr[5])); | ||
+ | printf("char[10] = %d[Byte]\n",sizeof(char[10])); | ||
+ | printf("\n"); | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | 上記プログラムの出力結果は以下です。 | ||
+ | <syntaxhighlight lang="text"> | ||
+ | sizeof演算子 | ||
+ | stDate構造体(int型*3個) = 12[Byte] | ||
+ | sizeof演算子戻り値のサイズ = 4[Byte] | ||
+ | |||
+ | int型 = 4[Byte] | ||
+ | short型 = 2[Byte] | ||
+ | char型 = 1[Byte] | ||
+ | long型 = 4[Byte] | ||
+ | double型 = 8[Byte] | ||
+ | float型 = 4[Byte] | ||
+ | bool型 = 1[Byte] | ||
+ | size_t型 = 4[Byte] | ||
+ | |||
+ | ポインタ(32bitアプリの場合) = 4[Byte] | ||
+ | </syntaxhighlight> |