struct-Body pointer

Source: Internet
Author: User
Tags mul

Main function

Main.m

C9_ structure pointer

//

Created by Dllo on 15/7/10.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "MyFunction.h"

A macro is a replacement process that replaces the contents of the following

# define PI 3.14

Macros with parameters

Calculates the two-digit and

# define MUL (A) (A) (A) * (b)

Parentheses are needed to prevent this problem from being affected by the precedence of the arithmetic notation.

# define MAXVALUE (A, b) a>b? A:b

First conditional compilation

#ifdef PI

#define TEST 20

#else

#define TEST 50

#endif

Second conditional compilation

#ifndef PI

#define TEST 20

#else

#define TEST 50

#endif

Third Kind

There are two kinds of conditions, one is 0, the other is 1.

#if 0

int a = 10;

#define TEST 20

#else

int a = 50;

#define TEST 50

#endif

int main (int argc, const char * argv[]) {

printf ("%ld\n", sizeof (STU));

//

Define a variable of struct type

Stu Stu = {70.5, ' w ', ' Zhangsan '};

With. To access member variables

printf ("%s\n", stu.stuname);

//

Structs can be assigned directly

Stu temp = Stu;

Constant address (array cannot be assigned directly)

int arr[2]={1,2};

x int Arr1[2]=arr;

Pointer variable

int *p =null;

int a = 10;

Char str[20]= "";

scanf ("%s", str);

printf ("%p\n", &a);

P =&a;

* Value Symbol

printf ("%d\n", *p);

//

int arr[5] ={3,4,6,5,1};

int *p=arr;

Bubble (P, 5);

for (int i =0; i<5; i++) {

printf ("%d\n", Arr[i]);

//    }

Custom data types

Stu Stu ={20, 70.5, ' W ', ' Yanglin '};

Stu *p =&stu;

printf ("%p\n", p);

printf ("%p\n", &stu);

P holds the address of the struct, and the result of the *p is equivalent to the variable of the struct that corresponds to the address, and then the usage is the same as the struct

printf ("%s\n", (*p). Stuname);

strcpy ((*p). Stuname, "Liushasha");

printf ("%s\n", (*p). Stuname);

//    //

printf ("%p\n", &stu.stuage);

printf ("%p\n", &stu.stuscore);

printf ("%s\n", p->stuname);

//

////

Cpoint m={8,11};

Cpoint n={5,7};

Cpoint *p1=&m;

Cpoint *p2=&n;

Distance (P1,P2);

//

Student Stu ={1, "L an OU", ' m ', 95.6};

Student *p=&stu;

printf ("%c\n", p->name[0]);

//

Change (p);

struct array

Student stu1 = {20,75.5, ' w ', ' Zhangsan '};

Student STU2 = {30,80.5, ' m ', ' Lisi '};

Student Stu3 = {33,90.5, ' w ', ' Wangwu '};

Student Stu4 = {23,87, ' m ', ' Yanglin '};

Student Stu[4] = {STU1,STU2,STU3,STU4};

printf ("%p\n", Stu);

printf ("%p\n", stu[0]);

Student *p = stu;

Stu[i] and p[i] like, the structure is taken out, so use. To find member variables

(p+1) is a pointer-based operation that accesses member variables

printf ("%s\n", p->stuname);

printf ("%s\n", p[0]);

With pointers to the way to traverse, all student names

for (int i =0; i<4; i++) {

printf ("%s\n", (p+i)->stuname);

//    }

//

According to the student's grade, the order is only for him, from the big to the small

for (int i =0; i<3; i++) {

for (int j=0; j<3-i; J + +) {

if (P[i].stuscore<p[i+1].stuscore) {

Student temp;

//

TEMP=P[J];

P[J]=P[J+1];

P[j+1]=temp;

//            }

//        }

//    }

for (int i =0; i<4; i++) {

printf ("%g\n", P[i].stuscore);

//    }

Average score and average age

int agecount = 0;

float Scorecount = 0;

for (int i =0; i<4; i++) {

Agecount + = (p+i)->stuage;

Scorecount + = (p+i)->stuscore;

//    }

printf ("%d\n", AGECOUNT/4);

Structure Array Exercises

Grade (&STU);

Student *p =&stu;

//

for (int i =0; i<4; i++) {

printf ("%g\n", P[i].stuscore);

//    }

Person per1={"Yanglin", 111,123,100};

Person per2={"Lishanshan", 222,234,200};

Person per3={"Shangshuai", 333,345,300};

Person per4={"Wangwu", 444,456,400};

Person Per[4]={per1,per2,per3,per4};

Enter the account password, correctly return the subscript, incorrect return 4

while (1) {

int entercard = 0;

scanf ("%d", &entercard);

int enterpassword = 0;

scanf ("%d", Enterpassword);

Person *p=per;

int result =checkpasscardandpassword (Entercard, Enterpassword, p, 4);

if (result = = 4) {

printf ("Error \ n");

Continue;

}else {

printf ("Login successful \ n");

//    }

//    }

Macro definition

Define a macro, name either all uppercase, or K + hump the Way

printf ("%g\n", pi+30);

int a =10, b = 20;

printf ("%d\n", MAXVALUE (A, b));

printf ("%d\n", MUL (3+4, b));

printf ("%d\n", TEST);

printf ("test =%d\n", a);

return 0;

}

. h file

//

MyFunction.h

C9_ structure pointer

//

Created by Dllo on 15/7/10.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

declaring struct bodies

Student: Name, age, gender, score, last use typedef

//

struct student{

int stuage;

float Stuscore;

Char Stusex;

Char stuname[20];//member variable

};

typedef struct student student;

struct student{

Long A;

char c;

//

Char D;

int b;

//};

typedef struct Student Stu;

Use a pointer to bubble sort

void Bubble (int *p,int count);

//

Prescribing

struct cpoint{

float x;

Float y;

//};

typedef struct CPOINT CPoint;

void distance (Cpoint *p1,cpoint *p2);

struct stu{

int num;

Char name[20];

char sex;

Float score;

//};

typedef struct STU Student;

void Change (Student *stu);

void Grade (Student *stu);

Name, account number, password, balance

struct person{

Char name[30];

int cardnum;

int PassWord;

int moneycount;

};

typedef struct person person;

int Checkpasscardandpassword (int entercard,int passWord, person *p,int count);

If the money is just taken away, the balance is zero, a "00" is appended to the name, and the amount of money taken away is modified and printed, and it needs to be judged not enough

int Checkmoney (int postion, person *p,int Getmoney);

. m file

/MYFUNCTION.M

C9_ structure pointer

//

Created by Dllo on 15/7/10.

Copyright (c) 2015 CML. All rights reserved.

//

#import "MyFunction.h"

Use a pointer to bubble sort

//

void Bubble (int *p,int count) {

for (int i =0; i<count-1; i++) {

for (int j=0; j<count-1-i; J + +) {

if (P[j]>p[j+1]) {//* (p+j) =p[j]

int temp =P[J];

P[J]=P[J+1];

P[j+1]=temp;

//            }

//        }

//    }

//}

//

void distance (Cpoint *p1,cpoint *p2) {

Fetch Address

float distance = 0;

//

Distance=sqrt (((p1->x)-(P2->X)) * ((p1->x)-(p2->x)) + ((p1->y)-(P2->y)) * ((P1->y)-(P2->y) ));

//

printf ("%g\n", distance);

//}

//

//

//

void Change (Student *stu) {

//

Determine if the first letter is lowercase

if (stu->name[0]>= ' a ' &&stu->name[0]<= ' Z ') {

Stu->name[0]-= (' A '-' a ');

//    }

for (int i =0; I<strlen (stu->name); i++) {

if (stu->name[i]== ') {

Stu->name[i]= ' _ ';

//        }

//    }

printf ("%s\n", stu->name);

//}

//

void Grade (Student *stu) {

//

for (int i =0; i<4; i++) {

if (stu[i].stusex== ' m ') {

Stu[i].stuscore +=10;

//        }

if (stu[i].stuscore>100) {

stu[i].stuscore=100;

//        }

//

//    }

//

//}

int Checkpasscardandpassword (int entercard,int passWord, person *p,int count) {

for (int i =0; i<count; i++) {

if ((Entercard = = P[i].cardnum) && (Password==p[i].password)) {

return i;

}

}

return 4;

}

int Checkmoney (int postion, person *p,int Getmoney) {

while (1) {

scanf ("%d", &getmoney);

if (Getmoney>p[postion].moneycount) {

printf ("The balance is insufficient, please re-enter \ n");

}else if (getmoney==p[postion].moneycount) {

The money's just gone.

strcat (P[postion].name, "00");

printf ("Balance:%d\n", P[postion].moneycount);

}else{

P[postion].moneycount-= Getmoney;

printf ("Balance:%d\n", P[postion].moneycount);

}

}

return 1;

}

struct-Body pointer

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.