全面詳解sizeof

#include <stdio.h>
using namespace std;

int main(void){
	printf("len_char=%d\nlen_int=%d\nlen_signed_int=%d\nlen_unsigned_int=%d\nlen_short=%d\nlen_long=%d\nlen_float=%d\nlen_double=%d\n", 
	sizeof(char),			// 1
	sizeof(int), 			// 4
	sizeof(signed int),		// 4
	sizeof(unsigned int),	// 4
	sizeof(short),			// 2
	sizeof(long),			// 4 (32位機)  or   8 (64位機)
	sizeof(float),			// 4
	sizeof(double)			// 8
	);
	
	int arr[] = { 1,2,3 };
	char str[] = "123";
	char *str2 = "123";
	int *p= arr;
	char *i=str;
	
	/*
	sizeof(arr), 	// 12 	3*int
	sizeof(str),  	// 4 	3*char+1('\0')
	sizeof(str2),   // 4	非數組指針 佔一個int (32位機) or 	8 (64位機)
	sizeof(*arr),  	// 4	int
	sizeof(*str),  	// 1	char
	sizeof(p),     	// 4	非數組指針 佔一個int (32位機) or 	8 (64位機)
	sizeof(i),     	// 4	非數組指針 佔一個int (32位機) or 	8 (64位機)
	sizeof(*p),    	// 4	int
	sizeof(*i)     	// 1	char
	*/
	
	printf("%d,%d,%d,%d,%d,%d,%d,%d,%d\n",sizeof(arr), sizeof(str), sizeof(str2),sizeof(*arr), sizeof(*str), sizeof(p), sizeof(i), sizeof(*p), sizeof(*i));
	return 0;
}

char *str2 = "123";char arr[] = {'1','2','3' };以及char arr[] = "123";是不一樣的!char *str2 = "123";char arr[] = "123";結尾都有一個'\0'!

具體參見博文
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章