/* How to use * and & */
int *b , a = 6, aa[a];// b is IP, a is value ,aa[a] is int array with size a
b = &a; //b=: the IP of a; '&a':IP of a. [*b=values, b=IP, a=values, &a=IP]
/* How to use sizeof() */
int a = 6;
sizeof(int); //the size of type int is 4 bytes
sizeof(a); // the size of variable 'a' is 4 bytes (int)
sizeof(xy); //the size of structure 'xy' is 12 bytes
struct xy
{
int x;
int y;
int z;
};
/* How to use malloc (size) ?
malloc is a function in stdlib.c 給出大小為size bytes的記憶體 no type!
本身是pointer所以只能配合指摽使用 */
struct xy *p = (xy*) malloc(sizeof(xy));
// malloc(sizeof(xy)) 給一塊記憶體 大小為type struct xy 的大小, 且 malloc(sizeof(xy))=pointer
// 給定 這塊記憶體 type 為 struct xy
// 有一個pointer p, type =struct xy, 且 *p (values)= (xy*) mall... (values)
ex:
struct xy *p = (xy*) malloc(sizeof(xy));
struct xy np;
std::cout <<"pointer:" <<sizeof(p)<< std::endl;
std::cout <<"values:"<<sizeof(*p)<< std::endl;
std::cout << "np:" << sizeof(np)<< std::endl;
pointer:8 //沒有指過去 所以print 紀錄一個pointer 變數 的大小
values:12//指過去了,所以print 指過去的記憶體大小
np:12
留言列表