结构体的定义与使用:

#include <stdio.h>
#include <stdlib.h>

struct Student
{
int num;
char name[30];
char age;
};


int main(int argc, char* argv[])
{
struct Student stu = { 1001, "lyshark", 22 };

printf("普通引用: %d --> %s \n", stu.num, stu.name);

struct Student *ptr; // 定义结构指针
ptr = &stu; // 指针的赋值

printf("指针引用: %d --> %s \n", ptr->num, ptr->name);


system("pause");
return 0;
}

动态分配结构体成员:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
struct Student
{
int num;
char name[30];
char age;
};

struct Student *stu = malloc(sizeof(struct Student));
stu->num = 1001;
stu->age = 24;
strcpy(stu->name, "lyshark");
printf("姓名: %s 年龄: %d \n", stu->name, stu->age);

// ----------------------------------------------------------
struct Person
{
char *name;
int age;
}person;

struct Person *ptr = &person;

ptr->name = (char *)malloc(sizeof(char)* 20);
strcpy(ptr->name, "lyshark");
ptr->age = 23;

printf("姓名: %s 年龄: %d \n", ptr->name, ptr->age);
free(ptr->name);

system("pause");
return 0;
}

结构体变量数组:

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int uid;
char name[64];
}Person;



void Print(struct Person *p,int len)
{
for (int x = 0; x < len; x++)
{
printf("%d \n", p[x].uid);
}
}

int main(int argc, char* argv[])
{
// 栈上分配结构体(聚合初始化)
struct Person p1[] = {
{ 1, "aaa" },
{ 2, "bbb" },
{ 3, "ccc" },
};

int len = sizeof(p1) / sizeof(struct Person);
Print(p1, len);

// 在堆上分配
struct Person *p2 = malloc(sizeof(struct Person) * 5);

for (int x = 0; x < 5; x++)
{
p2[x].uid = x;
strcpy(p2[x].name, "aaa");
}
Print(p2, 5);

system("pause");
return 0;
}

结构体深浅拷贝

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int uid;
char *name;
}Person;

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

struct Person p1,p2;

p1.name = malloc(sizeof(char)* 64);
strcpy(p1.name, "admin");
p1.uid = 1;

p2.name = malloc(sizeof(char)* 64);
strcpy(p2.name, "guest");
p2.uid = 2;

// p2 = p1; 浅拷贝

// 深拷贝

if (p1.name != NULL)
{
free(p1.name);
p1.name == NULL;
}

p1.name = malloc(strlen(p2.name) + 1);
strcpy(p2.name, p1.name);
p2.uid = p1.uid;

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

system("pause");
return 0;
}

结构体字段排序: 首先对比结构中的UID,通过冒泡排序将UID从小到大排列,也可以通过Name字段进行排序.

#include <stdio.h>
#include <stdlib.h>

struct Student
{
int uid;
char name[32];
double score;
};

int StructSort(struct Student *stu,int len)
{
for (int x = 0; x < len - 1; x++)
{
for (int y = 0; y < len - x - 1; y++)
{
// if (strcmp(stu[y].name, stu[y + 1].name) > 0)
if (stu[y].uid > stu[y + 1].uid)
{
// 结构体变量互换,将用户UID从小到大排列
struct Student tmp = stu[y];
stu[y] = stu[y + 1];
stu[y+1] = tmp;
}
}
}
return 0;
}

void MyPrint(struct Student *stu,int len)
{
for (int x = 0; x < len; x++)
printf("Uid: %d Name: %s Score: %.1f \n", stu[x].uid,stu[x].name,stu[x].score);
}

int main(int argc, char* argv[])
{
struct Student stu[3] = {
{8,"admin",79.5},
{5,"guest",89.5},
{1,"root",99},
};

StructSort(stu, 3); // 调用排序
MyPrint(stu, 3); // 输出结果

system("pause");
return 0;
}

结构体数据之间的交换:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Student
{
char *name;
int score[3];
};

int StructExchange(struct Student *stu, int len, char *str1,char *str2)
{
struct Student *ptr1;
struct Student *ptr2;

// 找到两个名字所对应的成绩
for (int x = 0; x < len; ++x)
{
if (!strcmp(stu[x].name, str1))
ptr1 = &stu[x];
if (!strcmp(stu[x].name, str2))
ptr2 = &stu[x];
}

// 开始交换两个人的成绩
for (int y = 0; y < 3; y++)
{
int tmp = ptr1->score[y];
ptr1->score[y] = ptr2->score[y];
ptr2->score[y] = tmp;
}
return 0;
}

void MyPrint(struct Student *stu,int len)
{
for (int x = 0; x < len; x++)
{
printf("Name: %s --> score: %d %d %d \n", stu[x].name, stu[x].score[0], stu[x].score[1], stu[x].score[2]);
}
}

int main(int argc, char* argv[])
{
struct Student stu[3];

// 动态开辟空间,并动态输入姓名与成绩
// admin 1 1 1 / guest 2 2 2 / root 3 3 3
for (int x = 0; x < 3; x++)
{
stu[x].name = (char *)malloc(sizeof(char) * 64); // 开辟空间
scanf("%s%d%d%d", stu[x].name, &stu[x].score[0], &stu[x].score[1], &stu[x].score[2]);
}

MyPrint(&stu, 3);
// 开始交换两个人名的成绩
StructExchange(&stu, 3, "root", "admin");
printf("----------------------------\n");

MyPrint(&stu, 3);

// 动态内存的释放
for (int y = 0; y < 3; y++)
free(stu[y].name);

system("pause");
return 0;
}

结构体偏移量计算:

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>

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

struct Student
{
int uid;
char *name;
};

struct Student stu = { 1, "lyshark" };
int offset = (int *)( (char *)&stu + offsetof(struct Student, name) );
printf("指针首地址: %x \n", offset);

// =================================================================
// 第二种嵌套结构体取地址

struct SuperClass
{
int uid;
char *name;
struct stu
{
int sid;
char *s_name;
}stu;
};

struct SuperClass super = { 1001, "lyshark" ,1,"xiaowang"};

int offset1 = offsetof(struct SuperClass, stu);
int offset2 = offsetof(struct stu, sid);

// SuperClass + stu 找到 sid 首地址
int struct_offset = ((char *)&super + offset1) + offset2;
printf("sid首地址: %x --> %x \n", struct_offset, &super.stu.sid);

int stu_sid = *(int *) ((char *)&super + offset1) + offset2;
printf("sid里面的数值是: %d \n", stu_sid);

int stu_sid_struct = ((struct stu *)((char *)&super + offset1))->sid;
printf("sid里面的数值是: %d \n", stu_sid_struct);

system("pause");
return 0;
}

结构体嵌套一级指针

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int id;
char *name;
int age;
}Person;

// 分配内存空间,每一个二级指针中存放一个一级指针
struct Person ** allocateSpace()
{
// 分配3个一级指针,每一个指针指向一个结构首地址
struct Person **tmp = malloc(sizeof(struct Person *) * 3);
for (int x = 0; x < 3; x++)
{
tmp[x] = malloc(sizeof(struct Person)); // (真正的)分配一个存储空间
tmp[x]->name = malloc(sizeof(char) * 64); // 分配存储name的空间
sprintf(tmp[x]->name, "name_%d", x);
tmp[x]->id = x;
tmp[x]->age = x + 10;
}
return tmp;
}

// 循环输出数据
void MyPrint(struct Person **person)
{
for (int x = 0; x < 3; x++)
{
printf("Name: %s \n", person[x]->name);
}
}

// 释放内存空间,从后向前,从小到大释放
void freeSpace(struct Person **person)
{
if (person != NULL)
{
for (int x = 0; x < 3; x++)
{
if (person[x]->name != NULL)
{
printf("%s 内存被释放 \n",person[x]->name);
free(person[x]->name);
person[x]->name = NULL;
}

free(person[x]);
person[x] = NULL;
}
free(person);
person = NULL;
}
}


int main(int argc, char* argv[])
{
struct Person **person = NULL;

person = allocateSpace();
MyPrint(person);
freeSpace(person);

system("pause");
return 0;
}

结构体嵌套二级指针

#include <stdio.h>
#include <stdlib.h>

struct Student
{
char * name;
}Student;

struct Teacher
{
char *name;
char **student;
}Teacher;

void allocateSpace(struct Teacher ***ptr)
{
// 首先分配三个二级指针,分别指向三个老师的结构首地址
struct Teacher **teacher_ptr = malloc(sizeof(struct Teacher *) * 3);

for (int x = 0; x < 3; x++)
{
// 先来分配老师姓名存储字符串,然后赋初值
teacher_ptr[x] = malloc(sizeof(struct Teacher)); // 给teacher_ptr整体分配空间
teacher_ptr[x]->name = malloc(sizeof(char)* 64); // 给teacher_ptr里面的name分配空间
sprintf(teacher_ptr[x]->name, "teacher_%d", x); // 分配好空间之后,将数据拷贝到name里面

// -------------------------------------------------------------------------------------
// 接着分配该老师管理的学生数据,默认管理四个学生
teacher_ptr[x]->student = malloc(sizeof(char *) * 4); // 给teacher_ptr 里面的student分配空间
for (int y = 0; y < 4; y++)
{
teacher_ptr[x]->student[y] = malloc(sizeof(char) * 64);
sprintf(teacher_ptr[x]->student[y], "%s_stu_%d", teacher_ptr[x]->name, y);
}
}
// 最后将结果抛出去
*ptr = teacher_ptr;
}

// 输出老师和学生数据
void MyPrint(struct Teacher **ptr)
{
for (int x = 0; x < 3; x++)
{
printf("老师姓名: %s \n", ptr[x]->name);
for (int y = 0; y < 4; y++)
{
printf("--> 学生: %s \n", ptr[x]->student[y]);
}
}
}

// 最后释放内存
void freeSpace(struct Teacher **ptr)
{
for (int x = 0; x < 3; x++)
{
if (ptr[x]->name != NULL)
{
free(ptr[x]->name);
ptr[x]->name = NULL;
}

for (int y = 0; y < 4; y++)
{
if (ptr[x]->student[y] != NULL)
{
free(ptr[x]->student[y]);
ptr[x]->student[y] = NULL;
}
}
free(ptr[x]->student);
ptr[x]->student = NULL;
}
}

int main(int argc, char* argv[])
{
struct Teacher **teacher_ptr = NULL;

allocateSpace(&teacher_ptr);
MyPrint(teacher_ptr);
freeSpace(teacher_ptr);

system("pause");
return 0;
}

结构体内嵌共用体:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct Person
{
int uid; // 编号
char name[20]; // 姓名
char jobs; // 老师=t 或 学生 = s
union
{
char stu_class[32]; // 学生所在班级
char tea_class[32]; // 老师的所教课程
}category;
};

int main(int argc, char* argv[])
{
struct Person person[3];

for (int x = 0; x < 3; x++)
{
// 首先输入前三项,因为这三个数据是通用的,老师学生都存在的属性
printf("输入: ID 姓名 工作类型(s/t) \n");
scanf("%d %s %c", &person[x].uid, &person[x].name, &person[x].jobs);

if (person[x].jobs == 's') // 如果是学生,输入stu_class
scanf("%s", person[x].category.stu_class);
if (person[x].jobs == 't') // 如果是老师,输入tea_class
scanf("%s", person[x].category.tea_class);
}

printf("--------------------------------------------------------------\n");

for (int y = 0; y < 3; y++)
{
if (person[y].jobs == 's')
printf("老师: %s 职务: %s \n", person[y].name, person[y].category.tea_class);
if (person[y].jobs == 't')
printf("学生: %s 班级: %s \n", person[y].name, person[y].category.stu_class);
}
system("pause");
return 0;
}

结构体与链表

结构体基本定义:

#include <stdio.h>

typedef struct Person
{
int uid;
char name[64];
}Person;

int main(int argc, char* argv[])
{
// 在栈上分配空间
struct Person s1 = { 100, "admin" };
printf("%s \n", s1.name);

// 在堆上分配空间
struct Person *s2 = malloc(sizeof(struct Person));
strcpy(s2->name, "lyshark");
printf("%s \n", s2->name);


system("pause");
return 0;
}

结构体变量数组:

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int uid;
char name[64];
}Person;



void Print(struct Person *p,int len)
{
for (int x = 0; x < len; x++)
{
printf("%d \n", p[x].uid);
}
}

int main(int argc, char* argv[])
{
// 栈上分配结构体(聚合初始化)
struct Person p1[] = {
{ 1, "aaa" },
{ 2, "bbb" },
{ 3, "ccc" },
};

int len = sizeof(p1) / sizeof(struct Person);
Print(p1, len);

// 在堆上分配
struct Person *p2 = malloc(sizeof(struct Person) * 5);

for (int x = 0; x < 5; x++)
{
p2[x].uid = x;
strcpy(p2[x].name, "aaa");
}
Print(p2, 5);

system("pause");
return 0;
}

结构体深浅拷贝

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int uid;
char *name;
}Person;

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

struct Person p1,p2;

p1.name = malloc(sizeof(char)* 64);
strcpy(p1.name, "admin");
p1.uid = 1;

p2.name = malloc(sizeof(char)* 64);
strcpy(p2.name, "guest");
p2.uid = 2;

// p2 = p1; 浅拷贝

// 深拷贝

if (p1.name != NULL)
{
free(p1.name);
p1.name == NULL;
}

p1.name = malloc(strlen(p2.name) + 1);
strcpy(p2.name, p1.name);
p2.uid = p1.uid;

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







system("pause");
return 0;
}

结构体嵌套一级指针

#include <stdio.h>
#include <stdlib.h>

typedef struct Person
{
int id;
char *name;
int age;
}Person;

// 分配内存空间,每一个二级指针中存放一个一级指针
struct Person ** allocateSpace()
{
// 分配3个一级指针,每一个指针指向一个结构首地址
struct Person **tmp = malloc(sizeof(struct Person *) * 3);
for (int x = 0; x < 3; x++)
{
tmp[x] = malloc(sizeof(struct Person)); // (真正的)分配一个存储空间
tmp[x]->name = malloc(sizeof(char) * 64); // 分配存储name的空间
sprintf(tmp[x]->name, "name_%d", x);
tmp[x]->id = x;
tmp[x]->age = x + 10;
}
return tmp;
}

// 循环输出数据
void MyPrint(struct Person **person)
{
for (int x = 0; x < 3; x++)
{
printf("Name: %s \n", person[x]->name);
}
}

// 释放内存空间,从后向前,从小到大释放
void freeSpace(struct Person **person)
{
if (person != NULL)
{
for (int x = 0; x < 3; x++)
{
if (person[x]->name != NULL)
{
printf("%s 内存被释放 \n",person[x]->name);
free(person[x]->name);
person[x]->name = NULL;
}

free(person[x]);
person[x] = NULL;
}
free(person);
person = NULL;
}
}

int main(int argc, char* argv[])
{
struct Person **person = NULL;

person = allocateSpace();
MyPrint(person);
freeSpace(person);

system("pause");
return 0;
}

C/C++ 结构体与指针笔记_数据

结构体嵌套二级指针

#include <stdio.h>
#include <stdlib.h>

struct Student
{
char * name;
}Student;

struct Teacher
{
char *name;
char **student;
}Teacher;

void allocateSpace(struct Teacher ***ptr)
{
// 首先分配三个二级指针,分别指向三个老师的结构首地址
struct Teacher **teacher_ptr = malloc(sizeof(struct Teacher *) * 3);

for (int x = 0; x < 3; x++)
{
// 先来分配老师姓名存储字符串,然后赋初值
teacher_ptr[x] = malloc(sizeof(struct Teacher)); // 给teacher_ptr整体分配空间
teacher_ptr[x]->name = malloc(sizeof(char)* 64); // 给teacher_ptr里面的name分配空间
sprintf(teacher_ptr[x]->name, "teacher_%d", x); // 分配好空间之后,将数据拷贝到name里面

// -------------------------------------------------------------------------------------
// 接着分配该老师管理的学生数据,默认管理四个学生
teacher_ptr[x]->student = malloc(sizeof(char *) * 4); // 给teacher_ptr 里面的student分配空间
for (int y = 0; y < 4; y++)
{
teacher_ptr[x]->student[y] = malloc(sizeof(char) * 64);
sprintf(teacher_ptr[x]->student[y], "%s_stu_%d", teacher_ptr[x]->name, y);
}
}
// 最后将结果抛出去
*ptr = teacher_ptr;
}

// 输出老师和学生数据
void MyPrint(struct Teacher **ptr)
{
for (int x = 0; x < 3; x++)
{
printf("老师姓名: %s \n", ptr[x]->name);
for (int y = 0; y < 4; y++)
{
printf("--> 学生: %s \n", ptr[x]->student[y]);
}
}
}

// 最后释放内存
void freeSpace(struct Teacher **ptr)
{
for (int x = 0; x < 3; x++)
{
if (ptr[x]->name != NULL)
{
free(ptr[x]->name);
ptr[x]->name = NULL;
}

for (int y = 0; y < 4; y++)
{
if (ptr[x]->student[y] != NULL)
{
free(ptr[x]->student[y]);
ptr[x]->student[y] = NULL;
}
}
free(ptr[x]->student);
ptr[x]->student = NULL;
}
}

int main(int argc, char* argv[])
{
struct Teacher **teacher_ptr = NULL;

allocateSpace(&teacher_ptr);
MyPrint(teacher_ptr);
freeSpace(teacher_ptr);

system("pause");
return 0;
}

C/C++ 结构体与指针笔记_#include_02

静态链表 理解一下

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点类型
struct LinkNode
{
int data;
struct LinkNode *next;
};


int main(int argc, char* argv[])
{
struct LinkNode node1 = { 10, NULL };
struct LinkNode node2 = { 20, NULL };
struct LinkNode node3 = { 30, NULL };
struct LinkNode node4 = { 40, NULL };

node1.next = &node2;
node2.next = &node3;
node3.next = &node4;
node4.next = NULL;

// 遍历链表结构

struct LinkNode *ptr = &node1;

while (ptr != NULL)
{
printf("%d \n", ptr->data);
ptr = ptr->next;
}

system("pause");
return 0;
}

动态链表

#include <stdio.h>
#include <stdlib.h>

// 定义链表节点类型
struct LinkNode
{
int data;
struct LinkNode *next;
};

struct LinkNode *init_link()
{ // 创建一个头结点,头结点不需要添加任何数据
struct LinkNode *header = malloc(sizeof(struct LinkNode));
header->data = 0;
header->next = NULL;

struct LinkNode *p_end = header; // 创建一个尾指针

int val = -1;
while (1)
{
scanf("%d", &val); // 输入插入的数据
if (val == -1) // 如果输入-1说明输入结束了
break;

// 先创建新节点
struct LinkNode *newnode = malloc(sizeof(struct LinkNode));
newnode->data = val;
newnode->next = NULL;

// 将节点插入到链表中
p_end->next = newnode;
// 更新尾部指针指向
p_end = newnode;
}
return header;
}

// 遍历链表
int foreach_link(struct LinkNode *header)
{
if (NULL == header || header->next == NULL)
return 0;

while (header->next != NULL)
{
printf("%d \n", header->data);
header = header->next;
}
return 1;
}

// 在header节点中oldval插入数据
void insert_link(struct LinkNode *header,int oldval,int newval)
{
struct LinkNode *pPrev = header;
struct LinkNode *Current = pPrev->next;

if (NULL == header)
return;


while (Current != NULL)
{
if (Current->data == oldval)
break;

pPrev = Current;
Current = Current->next;
}
// 如果值不存在则默认插入到尾部
//if (Current == NULL)
// return;

// 创建新节点

struct LinkNode *newnode = malloc(sizeof(struct LinkNode));
newnode->data = newval;
newnode->next = NULL;

// 新节点插入到链表中
newnode->next = Current;
pPrev->next = newnode;
}

// 清空链表
void clear_link(struct LinkNode *header)
{
// 辅助指针
struct LinkNode *Current = header->next;

while (Current != NULL)
{
// 保存下一个节点地址
struct LinkNode *pNext = Current->next;
printf("清空数据: %d \n", Current->data);
free(Current);
Current = pNext;
}
header->next = NULL;
}


// 删除值为val的节点
int remove_link(struct LinkNode *header, int delValue)
{
if (NULL == header)
return;

// 设置两个指针,指向头结点和尾结点
struct LinkNode *pPrev = header;
struct LinkNode *Current = pPrev->next;

while (Current != NULL)
{
if (Current->data == delValue)
{
// 删除节点的过程
pPrev->next = Current->next;
free(Current);
Current = NULL;
}
}

// 移动两个辅助指针
pPrev = Current;
Current = Current->next;


}

// 销毁链表
void destroy_link(struct LinkNode *header)
{
if (NULL == header)
return;

struct LinkNode *Curent = header;
while (Curent != NULL)
{
// 先来保存一下下一个节点地址
struct LinkNode *pNext = Curent->next;

free(Curent);

// 指针向后移动
Curent = pNext;
}
}

// 反响排序
void reverse_link(struct LinkNode *header)
{
if (NULL == header)
return;

struct LinkNode *pPrev = NULL;
struct LinkNode *Current = header->next;
struct LinkNode * pNext = NULL;

while (Current != NULL)
{
pNext = Current->next;
Current->next = pPrev;

pPrev = Current;
Current = pNext;
}
header->next = pPrev;
}

int main(int argc, char* argv[])
{
struct LinkNode * header = init_link();

reverse_link(header);
foreach_link(header);

clear_link(header);
system("pause");
return 0;
}

版权声明:本博客文章与代码均为学习时整理的笔记,文章 [均为原创] 作品,转载请 [添加出处] ,您添加出处是我创作的动力!