*************************************************************************************************************** ************
8. Use of the structure 2012.3.20
*************************************************************************************************************** ************
#include "string.h" #include "iostream" using namespace std;struct student{int Sid;char name[20];int age;};/ /structure end points can not be omitted ... void Main () {struct Student st = {7911, "lose",;p rintf ("%d%s %d\n", st.sid,st.name,st.age);//Control output format cout< <st.sid<< " " <<st.name<< " <<st.age<<endl;//control output Format//Modify the property value in the struct ST.SID = 95287;//st.name = "lost all";//The assignment of a string cannot be such an error!!! strcpy (St.name, "Lo");//The string is assigned a way to copy st.age = 99;printf ("%d %s %d\n", st.sid,st.name,st.age);}
struct Student st={1111111, "2222222", 3333333}
struct Student *pst=&st;
Then there are:
st.sid=100
pst->sid=100 two different ways effect same
************************************************************
#include "string.h" #include "iostream" using namespace std;struct student{int Sid;char name[20];int age;};/ /structure end points can not be omitted ... void f (struct Student *pst);//function to assign a value of void G1 (struct Student st), void G2 (struct Student *st); void main () {struct Stud ENT st;cout<< "Initial st" <<&st<<endl;f (&ST);//Method two outputs: G2 (&ST);//method one output, characterized by memory consumption, time G1 ( ST);//equivalent to open up a memory space}void f (struct Student *pst) {//pointer to the way of assignment pst->sid=1111111;strcpy (pst->name, "pst_name"); pst->age=200;cout<< "F *pst" <<& (*pst) <<endl;cout<< "F PST" <<pst<<endl;/ /Here is the content of the pointer
PST put is the address of the pointer is the address *PST is representative of the entire structure (not directly output, Cout<<*pst,error) & (*pst) is the first address of the removal structure (*PST) This value and the address value of the PST store is the same ... }void G2 (struct Student *st) {printf ("%d%s%d \ n", st->sid,st->name,st->age); St->sid = 666;//Method Two change the SID value Cout <<st->sid<<endl;cout<< "G2 *st" <<& (*st) <<endl;cout<< "G2 St" <<&am P;st<<endl;} void G1 (struct Student st) {printf ("%d%s%d \ n", St.sid,st.name,st.age);cout<< "G1 St" <<&st<<e ndl;//"The value of the SID is modified in method two, and the value in the second is not changed"}
Results:
Initial St 0012FF2C
F *pst 0012ff2c
F PST 0012ff2c
1111111 Pst_name 200
666
G2 *st 0012ff2c
G2 St 0012FEDC
666 Pst_name 200
G1 St 0012FEC4
8. Use of the structure 2015.12.3