结构体内存分配例子 win7-64 VS2015(32-bit)

本文展示了一个C语言中结构体内存对齐的例子,并通过一个具体实例解释了结构体成员如何影响整体大小及对齐方式。该程序使用VS2015 (32-bit) 编译环境下运行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
struct T {
	char a;
	int *d;
	int b;
	int c : 20;
	double e;
};

int main()
{
	T t = { 'A', NULL, 0x01020304, 0x123456, 15.0 };
	printf("sizeof(T)   = %d\n", sizeof(T));
	printf("sizeof(t.d) = %d\n", sizeof(t.d));
	printf("addr(t)     = %x\n", &t);
	printf("addr(t) %% 8 = %d\n", (unsigned long (&t)) % 8);
	return 0;
}

win7-64    VS2015(32-bit)


使用C语言,使用vscode软件,使用g++编译器,使用simplified chinese(gbk)编码环境,对于要存放系统代码的WJ文件夹,同目录下有.vscode文件夹,文件夹中有以下四个文件: c_cpp_properties.json内容为: { "configurations": [ { "name": "Win64", "includePath": ["${workspaceFolder}/**"], "defines": ["_DEBUG", "UNICODE", "_UNICODE"], "windowsSdkVersion": "10.0.18362.0", "compilerPath": "D:/MinGW/bin/g++.exe", "cStandard": "c17", "cppStandard": "c++17", "intelliSenseMode": "gcc-x64" } ], "version": 4 } launch.json内容为: { "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe", "preLaunchTask": "g++", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }, { "name": "C/C++: g++.exe 构建和调试活动文件", "type": "cppdbg", "request": "launch", "program": "${fileDirname}\\${fileBasenameNoExtension}.exe", "args": [], "stopAtEntry": false, "cwd": "D:/MinGW/bin", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "D:\\MinGW\\bin\\gdb.exe", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "将反汇编风格设置为 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ], "preLaunchTask": "C/C++: g++.exe 生成活动文件" } ] } settings.json内容为: { "files.associations": { "*.py": "python", "iostream": "cpp", "*.tcc": "cpp", "string": "cpp", "unordered_map": "cpp", "vector": "cpp", "ostream": "cpp", "new": "cpp", "typeinfo": "cpp", "deque": "cpp", "initializer_list": "cpp", "iosfwd": "cpp", "fstream": "cpp", "sstream": "cpp", "map": "c", "stdio.h": "c", "algorithm": "cpp", "atomic": "cpp", "bit": "cpp", "cctype": "cpp", "clocale": "cpp", "cmath": "cpp", "compare": "cpp", "concepts": "cpp", "cstddef": "cpp", "cstdint": "cpp", "cstdio": "cpp", "cstdlib": "cpp", "cstring": "cpp", "ctime": "cpp", "cwchar": "cpp", "exception": "cpp", "ios": "cpp", "istream": "cpp", "iterator": "cpp", "limits": "cpp", "memory": "cpp", "random": "cpp", "set": "cpp", "stack": "cpp", "stdexcept": "cpp", "streambuf": "cpp", "system_error": "cpp", "tuple": "cpp", "type_traits": "cpp", "utility": "cpp", "xfacet": "cpp", "xiosbase": "cpp", "xlocale": "cpp", "xlocinfo": "cpp", "xlocnum": "cpp", "xmemory": "cpp", "xstddef": "cpp", "xstring": "cpp", "xtr1common": "cpp", "xtree": "cpp", "xutility": "cpp", "stdlib.h": "c", "string.h": "c" }, "editor.suggest.snippetsPreventQuickSuggestions": false, "aiXcoder.showTrayIcon": true } tasks.json内容为: { "version": "2.0.0", "tasks": [ { "label": "g++", "command": "g++", "args": [ "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}.exe" ], "problemMatcher": { "owner": "cpp", "fileLocation": [ "relative", "${workspaceRoot}" ], "pattern": { "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$", "file": 1, "line": 2, "column": 3, "severity": 4, "message": 5 } }, "group": "build" }, { "type": "cppbuild", "label": "C/C++: g++.exe 生成活动文件", "command": "D:/MinGW/bin/g++.exe", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "D:/MinGW/bin" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ] } 系统代码文件为wj.c,代码为以下: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <windows.h> #define MAX_RECIPES 100 #define NAME_LEN 100 #define CATEGORY_LEN 50 #define INGREDIENTS_LEN 200 #define STEPS_LEN 500 #define LIKED_BY_LEN 100 // 定义菜谱结构体 typedef struct { char name[NAME_LEN]; char category[CATEGORY_LEN]; char ingredients[INGREDIENTS_LEN]; char steps[STEPS_LEN]; char liked_by[LIKED_BY_LEN]; } Recipe; Recipe recipes[MAX_RECIPES]; // 存储所有菜谱 int recipe_count = 0; // 当前菜谱数量 // 函数声明 void add_recipe(); void edit_recipe(); void delete_recipe(); void search_recipes(); void view_all_recipes(); void main_menu(); // 程序入口 int main() { SetConsoleOutputCP(936); // 设置控制台输出为 GBK SetConsoleCP(936); // 设置控制台输入为 GBK main_menu(); return 0; } // 主菜单函数定义 void main_menu() { int choice; while (1) { printf("\n--- 菜谱管理系统 ---\n"); printf("1. 添加菜谱\n"); printf("2. 修改菜谱\n"); printf("3. 删除菜谱\n"); printf("4. 查询菜谱\n"); printf("5. 查看全部菜谱\n"); printf("6. 退出系统"); printf("\n-------------------\n"); printf("请选择操作: "); scanf("%d", &choice); getchar(); switch (choice) { case 1: add_recipe(); break; case 2: edit_recipe(); break; case 3: delete_recipe(); break; case 4: search_recipes(); break; case 5: view_all_recipes(); break; case 6: exit(0); default: printf("无效选择,请重新输入。\n"); } } } // 添加菜谱 void add_recipe() { if (recipe_count >= MAX_RECIPES) { printf("菜谱已满,无法添加更多。\n"); return; } Recipe *r = &recipes[recipe_count++]; printf("请输入菜名: "); fgets(r->name, NAME_LEN, stdin); r->name[strcspn(r->name, "\n")] = '\0'; printf("请输入分类: "); fgets(r->category, CATEGORY_LEN, stdin); r->category[strcspn(r->category, "\n")] = '\0'; printf("请输入食材: "); fgets(r->ingredients, INGREDIENTS_LEN, stdin); r->ingredients[strcspn(r->ingredients, "\n")] = '\0'; printf("请输入制作步骤: "); fgets(r->steps, STEPS_LEN, stdin); r->steps[strcspn(r->steps, "\n")] = '\0'; printf("请输入谁喜欢吃: "); fgets(r->liked_by, LIKED_BY_LEN, stdin); r->liked_by[strcspn(r->liked_by, "\n")] = '\0'; printf("菜谱添加成功!\n"); } // 修改菜谱 void edit_recipe() { char name[NAME_LEN]; printf("请输入要修改的菜名: "); fgets(name, NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; for (int i = 0; i < recipe_count; i++) { if (strcmp(recipes[i].name, name) == 0) { int field; printf("请选择要修改的字段:\n"); printf("1. 分类\n2. 食材\n3. 制作步骤\n4. 谁喜欢吃\n"); scanf("%d", &field); getchar(); switch (field) { case 1: printf("请输入新分类: "); fgets(recipes[i].category, CATEGORY_LEN, stdin); recipes[i].category[strcspn(recipes[i].category, "\n")] = '\0'; break; case 2: printf("请输入新食材: "); fgets(recipes[i].ingredients, INGREDIENTS_LEN, stdin); recipes[i].ingredients[strcspn(recipes[i].ingredients, "\n")] = '\0'; break; case 3: printf("请输入新制作步骤: "); fgets(recipes[i].steps, STEPS_LEN, stdin); recipes[i].steps[strcspn(recipes[i].steps, "\n")] = '\0'; break; case 4: printf("请输入新的喜好者: "); fgets(recipes[i].liked_by, LIKED_BY_LEN, stdin); recipes[i].liked_by[strcspn(recipes[i].liked_by, "\n")] = '\0'; break; default: printf("无效选择。\n"); } printf("菜谱信息已更新。\n"); return; } } printf("未找到该菜名的菜谱。\n"); } // 删除菜谱 void delete_recipe() { char name[NAME_LEN]; printf("请输入要删除的菜名: "); fgets(name, NAME_LEN, stdin); name[strcspn(name, "\n")] = '\0'; for (int i = 0; i < recipe_count; i++) { if (strcmp(recipes[i].name, name) == 0) { for (int j = i; j < recipe_count - 1; j++) { recipes[j] = recipes[j + 1]; } recipe_count--; printf("菜谱删除成功。\n"); return; } } printf("未找到该菜名的菜谱。\n"); } // 查询菜谱 void search_recipes() { int choice; printf("请选择查询方式:\n"); printf("1. 按菜名\n2. 按分类\n3. 按食材\n4. 按谁喜欢吃\n"); scanf("%d", &choice); getchar(); char keyword[NAME_LEN]; printf("请输入菜名: "); fgets(keyword, NAME_LEN, stdin); keyword[strcspn(keyword, "\n")] = '\0'; int found = 0; for (int i = 0; i < recipe_count; i++) { switch (choice) { case 1: if (strstr(recipes[i].name, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 2: if (strstr(recipes[i].category, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 3: if (strstr(recipes[i].ingredients, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; case 4: if (strstr(recipes[i].liked_by, keyword)) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); found = 1; } break; } } if (!found) printf("未找到匹配的菜谱。\n"); } // 查看全部菜谱 void view_all_recipes() { if (recipe_count == 0) { printf("当前没有菜谱。\n"); return; } for (int i = 0; i < recipe_count; i++) { printf("菜名: %s\n分类: %s\n食材: %s\n制作步骤: %s\n谁喜欢吃: %s\n\n", recipes[i].name, recipes[i].category, recipes[i].ingredients, recipes[i].steps, recipes[i].liked_by); } } 将这个wj.c文件的系统代码,改成c和h文件,并给出每个文件中完整的代码和注释
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值