forked from ShiqiYu/CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer-struct.cpp
39 lines (30 loc) · 992 Bytes
/
pointer-struct.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <cstring>
using namespace std;
struct Student
{
char name[4];
int born;
bool male;
};
int main()
{
Student stu = {"Yu", 2000, true};
Student * pStu = &stu;
cout << stu.name << " was born in " << stu.born
<< ". Gender: " << (stu.male ? "male" : "female") << endl;
strncpy(pStu->name, "Li", 4);
pStu->born = 2001;
(*pStu).born = 2002;
pStu->male = false;
cout << stu.name << " was born in " << stu.born
<< ". Gender: " << (stu.male ? "male" : "female") << endl;
printf("Address of stu: %p\n", pStu); //C style
cout << "Address of stu: " << pStu << endl; //C++ style
cout << "Address of stu: " << &stu << endl;
cout << "Address of member name: " << &(pStu->name) << endl;
cout << "Address of member born: " << &(pStu->born) << endl;
cout << "Address of member male: " << &(pStu->male) << endl;
cout << "sizeof(pStu) = " << sizeof(pStu) << endl;
return 0;
}