Tuesday, January 31, 2012

C/C++ Exercise: sizeof() and pointer

C/C++ Exercise: sizeof() and pointer
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;
using std::endl;

int main(){
 cout << "http://dev-microsoft.blogspot.com/" << endl;
 cout << "C/C++ Exercise: sizeof" << endl << endl;

 cout << "sizeof(int): " << sizeof(int) << endl;
 cout << "sizeof(char): " << sizeof(char) << endl;
 cout << endl;

 //Array
 char chArray[10];
 cout << "sizeof array: " << sizeof(chArray) << endl;
 cout << "sizeof a element in array: " << sizeof(chArray[0]) << endl;
 cout << "number of element in array: " << sizeof(chArray)/sizeof(chArray[0]) << endl;
 cout << endl;

 //pointer to int
 int i = 7;
 int* ptInt = &i;
 cout << "ptInt = " << ptInt << endl;
 cout << "*ptInt = " << *ptInt << endl;
 cout << "sizeof(ptInt): " << sizeof(ptInt) << endl;
 cout << "sizeof(*ptInt): " << sizeof(*ptInt) << endl;
 cout << endl;

 //pointer to char
 char* ptChar ("Hello World!");
 cout << "ptChar = " << ptChar << endl;
 cout << "*ptChar = " << *ptChar << endl;
 cout << "sizeof(ptchar): " << sizeof(ptChar) << endl;
 cout << "sizeof(*ptchar): " << sizeof(*ptChar) << endl;
 cout << endl;

 cout << endl;
 system("PAUSE");
 return 0;

}

No comments:

Post a Comment