本文主要是介绍C语言基础(十七),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
C语言中的结构体(Struct)是一种用户自定义的数据类型,允许将不同类型的数据项组合成一个单一的类型:
测试代码1:
#include "date.h"
#include <stdio.h>
#include <string.h>
// 定义结构体数组
typedef struct { int id; char name[20];
} Person; // 使用函数初始化Person结构体的函数
void initPersonFunc(Person *p, int id, const char *name) { p->id = id; strcpy(p->name, name);
} int main() { int time = getTime();// 声明时初始化 Person people1[] = { {1, "Alice"}, {2, "Bob"} }; // 打印people1数组 printf("Using declaration initialization:\n"); for (int i = 0; i < 2; i++) { printf("ID: %d, Name: %s\n", people1[i].id, people1[i].name); } // 使用循环或条件语句初始化 Person people2[3]; for (int i = 0; i < 3; i++) { people2[i].id = i + 3; // 假设ID从3开始 sprintf(people2[i].name, "Charlie%d", i + 1); // 构造名字 } // 打印people2数组 printf("\nUsing loop initialization:\n"); for (int i = 0; i < 3; i++) { printf("ID: %d, Name: %s\n", people2[i].id, people2[i].name); } // 使用函数初始化 Person person3; initPersonFunc(&person3, 101, "David"); // 打印person3 printf("\nUsing function initialization:\n"); printf("ID: %d, Name: %s\n", person3.id, person3.name); return 0;
}
运行结果如下:
测试代码2:
#include "date.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // 定义结构体
typedef struct { char name[50]; int age;
} Person; int main() { int time = getTime();// 创建结构体变量 Person person1 = {"Alice", 30}; // 创建一个指向结构体的指针,并将其初始化为指向person1 Person *ptr = &person1; // 使用指针访问结构体成员 printf("Name: %s, Age: %d\n", ptr->name, ptr->age); // 修改结构体成员的值 ptr->age = 31; // 打印 printf("After modification: Name: %s, Age: %d\n", ptr->name, ptr->age); // 使用动态内存分配 Person *person2 = (Person *)malloc(sizeof(Person)); if (person2 != NULL) { strcpy(person2->name, "Bob"); person2->age = 25; // 使用指针访问动态分配的结构体成员 printf("Dynamic Person: Name: %s, Age: %d\n", person2->name, person2->age); // 释放动态分配的内存 free(person2); } return 0;
}
运行结果如下:
测试代码3:
#include "date.h"
#include <stdio.h>
#include <string.h> // 定义Phone结构体
struct Phone { char brand[50]; char model[50]; int year;
}; // 通过值传递打印Phone信息
void printPhoneByValue(struct Phone phone) { printf("通过值传递: Brand: %s, Model: %s, Year: %d\n", phone.brand, phone.model, phone.year);
} // 通过指针传递更新Phone的年份并打印
void updateAndPrintPhoneByPointer(struct Phone *phone, int newYear) { phone->year = newYear; // 更新年份 printf("通过指针传递: Brand: %s, Model: %s, Year: %d\n", phone->brand, phone->model, phone->year);
} // 确认Phone的状态
void printPhoneAgainByPointer(struct Phone *phone) { printf("再次确认通过指针传递后的状态: Brand: %s, Model: %s, Year: %d\n", phone->brand, phone->model, phone->year);
} int main() { int time = getTime();// 创建一个Phone结构体 struct Phone myPhone = {"Apple", "iPhone 12", 2020}; // 通过值传递打印Phone信息 printPhoneByValue(myPhone); // 通过指针传递更新Phone的年份并打印 updateAndPrintPhoneByPointer(&myPhone, 2021); // myPhone的年份更新 printPhoneAgainByPointer(&myPhone); return 0;
}
运行结果如下:
测试代码4:
#include <stdio.h>
#include <string.h>
#include "date.h"
#include <stdlib.h>// 定义一个枚举类型
enum Brand { Apple, Samsung, Xiaomi, Huawei, Others };// 定义一个结构体Phone
struct Phone {char model[50]; // 字符串,表示手机型号int year; // 整数,表示发布年份float screenSize; // 浮点数,表示屏幕尺寸double price; // 双精度浮点数,表示价格enum Brand brand; // 枚举类型,表示品牌char *color; // 字符指针,指向动态分配的颜色字符串
};// 初始化手机结构体的函数
void initPhone(struct Phone *phone, const char *model, int year, float screenSize, double price, enum Brand brand, const char *color) {strcpy(phone->model, model);phone->year = year;phone->screenSize = screenSize;phone->price = price;phone->brand = brand;phone->color = strdup(color); // 使用strdup复制字符串,需要释放内存
}// 释放颜色字符串占用的内存
void freePhone(struct Phone *phone) {free(phone->color);
}// 打印手机信息的函数
void printPhone(const struct Phone *phone) {printf("Model: %s\n", phone->model);printf("Year: %d\n", phone->year);printf("Screen Size: %.2f inches\n", phone->screenSize);printf("Price: %.2f\n", phone->price);printf("Brand:");switch(phone->brand) {case Apple: printf("Apple\n"); break;case Samsung: printf("Samsung\n"); break;case Xiaomi: printf("Xiaomi\n"); break;case Huawei: printf("Huawei\n"); break;case Others: printf("Others\n"); break;}printf("Color: %s\n", phone->color);
}int main() {int time = getTime();struct Phone myPhone;initPhone(&myPhone, "iPhone 12", 2020, 6.1, 999.99, Apple, "Space Gray");printPhone(&myPhone);freePhone(&myPhone); // 释放颜色字符串占用的内存return 0;
}
运行结果如下:
这篇关于C语言基础(十七)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!