结构体_结构体指针,tyepdef_union_enum

来源:互联网 发布:西门淘宝店网址 编辑:程序博客网 时间:2024/06/05 14:55
#include "stdafx.h"#include <stdlib.h>  #include <windows.h>#include <string.h>struct Student{char *name;int age;};//锁定结构体的变量的数量struct{char name[20];int age;int classId;};//结构体定义和初始化int main(){int a = 10;int b;b = 20;struct Student stu1 = { "lucy", 20 };//char * name=“lucy";这样"lucy"就是常量  char name[20] ="lucy";就不是常量//struct Student stu2;//把c复制到a中就可以这样用:strcpy(a,c);/*char str[10] = "lucy1";char aa[5] = "a";char bb[5] = "b";strcpy(aa, bb);*/strcpy(stu1.name, "lll");stu1.age = 20;printf("%s, %d \n", stu1.name, stu1.age);system("pause");return 0;}//结构体数组int main(){int i;struct Student stu[3] = { { "lucy", 20 }, { "lilei", 32 }, { "hanmeimei", 35 } };printf("stu address:%#x\n", stu);struct Student s[5];for (i = 0; i < 5; i++){s[i].age = 10 + i;s[i].name = (char *)malloc(10);//s[i].name = "lucy";//错误2error C2440: “=”: 无法从“const char [6]”转换为“char [10]”strcpy(s[i].name, "lucy");}for (i = 0; i < 5; i++){printf("s %d: %s, %d\n", i, s[i].name, s[i].age);}int b = 20;int * a = &b;//struct Student * stud = stu;struct Student *stud;stud = (Student *)malloc(sizeof(Student)* 4);/*0x004fa020 {name = 0x004fa020 "屯屯屯屯屯 age=-842150451 }name: 0x004fa020 "屯屯屯屯屯  age : -842150451*/printf("%#x\n", stud);memset(stud, 0, sizeof(struct Student) * 4);//Memset 用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘ ’或‘/0’;/*0x004fa020 {name = 0x004fa020 "" age = 0 }name: 0x004fa020 ""age : 0*/for (i = 0; i < 4; i++){//(stud + i)->age = 20 + i;////(stud + i)->name = "lucy";//strcpy((stud + i)->name, "lucy");stud[i].age = 20 + i;s[i].name = (char *)malloc(10);//分配一块内存strcpy(stud[i].name, "lucy");}for (i = 0; i < 4; i++){printf("stud %d:%s,%d\n", i, (stud + i)->name, (stud + i)->age);}system("pause");return 0;}struct Man{int age;char *name;int(*Msg)(char *, int);};int message(char * str, int age){MessageBox(0, TEXT("hello"), TEXT("Lijian"), 0);return 0;}int main(){struct Man man;man.age = 40;man.name = "李健";man.Msg = message;man.Msg(man.name, man.age);system("pause");return 0;}//结构体中添加结构体指针成员变量struct Node {int data;Node * next;};// 在单链表的末尾添加一个数据int enqueNode(Node *head, int data) {Node * node = (Node *)malloc(sizeof(Node));if (node == NULL) {return 0;}node->data = data;node->next = NULL;Node *p = head;while(p->next != NULL) {p = p->next;}p->next = node;    /*while (head->next != NULL) {head++;}*/    return 1;}int main(){int num = 10;int i = 0;Node * list;list = (Node *)malloc(sizeof(struct Node));list->data = 0;list->next = NULL;for (i = 0; i < num; i++) {enqueNode(list, i+1);}/*list0x0041bcf0 {data=0 next=0x0041bd38 {data=1 next=0x0041bd80 {data=2 next=0x0041bdc8 {data=3 next=0x0041be10 {...} } } } }    data: 0    next: 0x0041bd38 {data=1 next=0x0041bd80 {data=2 next=0x0041bdc8 {data=3 next=0x0041be10 {data=4 next=0x0041be58 {...} } } } }*/while (list->next != NULL){printf("%d \n", list->data);list = list->next;}system("pause");return 0;}//typedef指令typedef int _in;typedef char * string;typedef int (* PFI)(char *, char *);typedef struct Tnode {char *word;int count;/*Tnode * left;Tnode * right;Treeptr left;Treeptr right;*/} BinaryTreeNode;typedef Tnode * Treeptr;int fun(char *, char *) {return 0;}int main(){_in a = 20;printf("%d\n", a);//20string str;str = "hello world";PFI fp;fp = fun;char * ch;ch = "hello world";BinaryTreeNode * node;node = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));system("pause");return 0;}//公用体union myunion{int a;char b;float c;};int main() {myunion unio;unio.a = 10;unio.b = 'b';unio.c = 1.2f;printf("a: %#x, b: %#x, c: %#x\n", &unio.a, &unio.b, &unio.c);printf("a: %d, b: %c, c: %f\n", unio.a, unio.b, unio.c);system("pause");return 0;}enum {monday = 10,    saturday,    sunday,};enum weekday { sun, mon, tue, wed, thu, fri, sat } day;int main() {int k;printf("input a number(0--6)");scanf("%d", &k);day = (enum weekday)k;switch (day){case sun: printf("sunday\n"); break;case mon: printf("monday\n"); break;case tue: printf("tuesday\n"); break;case wed:printf("wednesday\n"); break;case thu:printf("thursday\n"); break;case fri: printf("friday\n"); break;case sat: printf("satday\n"); break;default: printf("input error\n"); break;}system("pause");}

原创粉丝点击