8. Use of the structure 2015.12.3

Source: Internet
Author: User

*************************************************************************************************************** ************

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 (&AMP;ST);//Method two outputs: G2 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.