網站首頁 編程語言 正文
前景提示
定義一個結構體,結構體中有兩個變量,其中一個是char類型的數組,那么,怎么向這個數組中插入數據,打印數據呢?
typedef struct SequenceList { // 數組的元素 char element[20]; // 數組的長度 int length; };
定義一個結構體,結構體中有兩個變量,其中一個是char類型的數組指針,那么,怎么向這個數組中插入數據,打印數據呢?
// 定義順序表結構體 typedef struct SequenceList { char *elment; int length; };
這里的結構體處理的步驟
- 結構體初始化
- 結構體內數據賦值
- 結構體內輸出數據
本著上述的原則,先對第一種類型進行操作
一.char數組類型的處理
1.結構體初始化
SequenceList L; L.element = (char*)malloc(sizeof(char)*10); L.length = 10
2.結構體內數據賦值(簡單法)
L.elment[0] = 1; L.elment[1] = 2; L.elment[2] = 3; L.elment[3] = 4; L.elment[4] = 5;
for循環
for (int i = 0; i < 10; i++) { L.elment[i] = i+1; }
3.結構體內輸出數據
for (int i = 0; i < 10; i++) { //不會打印空值 if (L.elment[i]>0) { printf("element[%d] = %d\n",i, L.elment[i]); } }
二.char數組指針類型的處理
1.結構體初始化
//結構體初始化 MyList L; L.length = LENGTH; L.elment = (char*)malloc(L.length * sizeof(char));
2.結構體內數據賦值
//結構體賦值 for (int i = 0; i < LENGTH; i++) { *(L.elment + i) = 'A' + i; }
3.結構體內輸出數據
//打印結構體中的值 for (int i = 0; i < LENGTH; i++) { if (*(L.elment + i) > 0) { printf("elment[%d] = %c\n", i, *(L.elment + i)); } }
三.全部代碼
1. char數組
// 010.順序表_004.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。 // #include#define MAXSIZE 10 typedef struct SequenceList { // 數組的元素 char element[MAXSIZE]; // 數組的長度 int length; }; int main() { // 1.初始化結構體 SequenceList *L; L = (SequenceList*)malloc(sizeof(char)*MAXSIZE); L->length = MAXSIZE; // 2.存入結構體內值 for (int i = 0; i < MAXSIZE; i++) { L->element[i] = 'a' + i; } // 3.打印結構體內的值 for (int i = 0; i < MAXSIZE; i++) { if (*(L->element + i) > 0) { printf("elment[%d] = %c\n", i, *(L->element + i)); } } }
2. char數組指針
// 011.順序表_005.cpp : 此文件包含 "main" 函數。程序執行將在此處開始并結束。 // #include#define MAXSIZE 10 typedef struct SequenceList { // 數組的元素 char *element; // 數組的長度 int length; }; int main() { // 1.結構體初始化 SequenceList L; L.length = MAXSIZE; L.element = (char*)malloc(L.length * sizeof(MAXSIZE)); // 2.結構體內賦值 for (int i = 0; i < MAXSIZE; i++) { *(L.element + i) = 'a' + i; } // 3.打印結構體中的值 for (int i = 0; i < MAXSIZE; i++) { if (*(L.element + i) > 0) { printf("elment[%d] = %c\n", i, *(L.element + i)); } } }
結語這就是最近遇到的問題,這個問題困擾了很久,相信許多的初學者也遇到了這樣的問題,但是,網上的描述根本不怎么好用,所以,希望本博主遇到的這個問題能幫助到你
總結
原文鏈接:https://www.cnblogs.com/liuyangfirst/p/15964945.html
相關推薦
- 2022-03-27 Android實現井字游戲_Android
- 2022-10-17 Python可視化程序調用流程解析_python
- 2023-02-26 C++?ROS與boost:bind()使用詳解_C 語言
- 2022-04-03 帶你理解C語言中的漢諾塔公式_C 語言
- 2022-06-04 Kubernetes中Deployment的升級與回滾_云和虛擬化
- 2021-12-07 Linux系統的修復模式(單用戶模式)_Linux
- 2022-04-10 Blazor路由與頁面導航開發介紹_基礎應用
- 2022-10-06 Django數據映射(一對一,一對多,多對多)_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支