struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
struct Student {
char name[20];
int age;
}Lucy = { "lucy", 20 }; 全局的变量
int main() {
方法内的定义,调用和初始化
Student stu = { "jason", 18 };
printf("name: %s, age %d \n", stu.name, stu.age);
struct Student stu2;
strcpy(stu2.name, "123");
stu2.age = 20;
printf("stu2.name = %s, stu2.age= %d \n", stu2.name, stu2.age);
system("pause");
return 0;
}
struct{
char name[20];
int age;
int classId;
}stu3,stu4,stu5;
struct Student {
char * name;
int age;
}Lucy = { "lucy", 20 };
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 = 20 + i;
//strcpy(s[i].name, "lucy"); //数组不能直接赋值
s[i].name = "lucy"; //如果name是指针,可以直接赋值
}
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(struct Student) * 4);
printf("%#x\n", stud);
memset(stud, 0, sizeof(struct Student) * 4); //将当前内存块的内容全部初始化位0
for (i = 0; i < 4; i++) {
/*(stud + i)->age = 20 + i;
(stud + i)->name = "lucy";*/
stud[i].age = 20 + i;
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;
}
memset的方法:https://cloud.tencent.com/developer/article/1646115
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; //创建一个指针指向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);
}
while (list->next != NULL)
{
printf("%d \n", list->data);
list = list->next;
}
system("pause");
return 0;
}
双向链表:
struct DoubleLink {
int value;
DoubleLink *pre;
DoubleLink *next;
};
int insertNode(DoubleLink *head, int index, int value) {
DoubleLink *node = (DoubleLink *)malloc(sizeof(DoubleLink));
if (node == NULL) {
return 0;
}
node->value = value;
node->next = NULL;
node->pre = NULL;
//创建一个索引指针,指向head位置
DoubleLink *p = head;
//链表当前的数量
int length = 1;
while (p ->next != NULL)
{
p = p->next;
length++;
}
if (index < 0 || index > length) {
return 0;
}
if (length == index) {
head->next = node;
node->pre = head;
node->next = NULL;
return 0;
}
DoubleLink *p1 = head;
int i;
while (p1->next != NULL)
{
if(i = index)
{
//p1->next->pre = node;
//p1->next = node;
//node->next = p1->next;
//node->pre = p1;
p1 = p1->next; //先拿到插入前的下一个节点
node->next = p1;
node->pre = p1->pre;
p1->pre->next = node;
p1->pre = node;
break;
}
else
{
p1 = p1->next;
i++;
}
}
return 0;
}
int deleteNode(DoubleLink *head, int index) {
DoubleLink *p = head;
//链表当前的数量
int length = 1;
while (p->next != NULL)
{
p = p->next;
length++;
}
if (length == 1 || index < 0 || index >= length)
{
//表示空链表
return 0;
}
else
{
DoubleLink *deletenode = head;
for (int i = 0; i<index; i++)
{
deletenode = deletenode->next;
}
deletenode->pre->next = deletenode->next;
deletenode->next->pre = deletenode->pre;
delete deletenode;
}
return 0;
}
int main() {
DoubleLink * LinkList;
LinkList = (DoubleLink *)malloc(sizeof(struct DoubleLink));
LinkList->value = 1;
LinkList->next = NULL;
LinkList->pre = NULL;
insertNode(LinkList, 1, 1);
insertNode(LinkList, 1, 2);
insertNode(LinkList, 1, 3);
insertNode(LinkList, 2, 4);
deleteNode(LinkList, 3);
while (LinkList->next != NULL)
{
printf("%d \n", LinkList->value);
LinkList = LinkList->next;
}
system("pause");
return 0;
}
共用体是表示:公用体内的所有变量的是公用一块内存的,当前内存是当中某个占用的最大内存。即,将不同的数据类型的数据放到同一段内存里面。
union MyUnion
{
int a;
char b;
float c;
};
int main() {
MyUnion unio;
unio.a = 10;
unio.b = 'a';
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;
}
typedef 就是用来为复杂的声明定义简单的别名
类似java 代理
并没有创建新的数据类型,只是给现有类型创建了别名
typedef int _in;
typedef char * string;
typedef int(*PFI)(char *, char *);
typedef Tnode * Treeptr;
typedef struct Tnode {
char *word;
int count;
/*Tnode * left;
Tnode * right;*/
Treeptr left;
Treeptr right;
} BinaryTreeNode;
int fun(char *, char *) {
return 0;
}
int main() {
_in a = 20;
printf("%d\n", a);
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;
}
vs215无法使用strcpy的问题: