C9-高級指針

一、指針變量的定義和使用
二、取值運算符
三、取值運算符
四、指針的算術運算

代碼:
typedef struct student {
char sex;
int num;
float score;
} Student;

typedef struct CPoint {
float x;
float y;
} CP;

typedef struct teacher {
char name[10];
char sex;
float score;
} Teacher;

 //命名:全大寫 或 k+駝峯命名法

define STUNUM 2000

define kMax 2000

define MUL(a, b) ((a) * (b))

define test1 int *

typedef int * test2;

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

// Student stu = {‘m’, 3, 99.9};
// // 結構體指針的大小與結構體的大小匹配
// Student *p = NULL;
//
// // 指向結構體變量
// p = &stu;
// // 不能通過指針一次性打印結構體中所有成員
// // printf(“%c\n”, *p);
//
// // 通過指針訪問結構體中的成員
// printf(“%d\n”, p->num);
// printf(“%c\n”, p->sex);
//
// printf(“%c\n”, (*p).sex);

// CP c = {3, 4};
// CP d = {9, 12};
// CP *p = NULL;
// CP *q = NULL;
// p = &c;
// q = &d;
// float sum1 = 0;
// sum1 = (((p).x - q->x) ((p).x - q->x)) + (((*p).y - q->y) ((*p).y - q->y));
//
// sum1 = sqrt(sum1);
//
// printf(“%.2f “, sum1);

// 數組指針。 男生成績+10,超過100則打印100

// Student a[] = {{“zhangsan”, ‘m’, 80}, {“lisi”, ‘f’, 80}, {“wangwu”, ‘m’, 99}, {“zhaoliu”, ‘m’, 60}};
// int count = sizeof(a) / sizeof(a[0]);
// Student *p = NULL;
// p = a;
// for (int i = 0; i < count; i++) {
// if (‘m’ == p[i].sex) {
// p[i].score += 10;
// if (p[i].score > 100) {
// p[i].score = 100;
// }
// printf(“%s %c %.2f\n”, p[i].name, p[i].sex, p[i].score);
// }
// }

//  宏

// Student stu[3] = {
// {“zhangsan”, ‘m’, 99.9},
// {“lisi”, ‘f’, 88.8},
// {“wangwu”, ‘m’, 77.7}
// };
// Student *p = NULL;
// p = stu;
// p++;
// printf(“%.2f\n”, (*(p + 1)).score);

// int ret = MUL(3 + 7, 5 + 8);
// printf(“%d\n”, ret);

// 注意:用typedef和#define交換指針類型後,指針使用的影響
// typedef是類型交換,define是將替換的內容完全移動到主函數中調用的位置

// int *p = NULL;
// int *q = NULL;
// int *p, *q;
//
// int a = 3;
// test1 a1 = NULL, a2 = NULL;
// test2 b1 = NULL, b2 = NULL;
// a1 = &a;
// a2 = &a;
// b1 = &a;
// b2 = &a;
// printf(“%d “, *a1);
// printf(“%d “, *a2);
// printf(“%d “, *b1);
// printf(“%d “, *b2);

//#if 0
// for (int i = 0; i < 5; i++) {
// printf(“a “);
// }
//#else
// for (int i = 0; i < 5; i++) {
// printf(“b “);
// }
//#endif
//

return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章