1. 程式人生 > >結構體_結構體指標,tyepdef_union_enum

結構體_結構體指標,tyepdef_union_enum

#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";//錯誤	2	error 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);
	}
	/*
	list
0x0041bcf0 {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);//20

	string 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");
}