In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the C language structure in the char array how to assign, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
Foreground hint
Define a structure in which there are two variables, one of which is an array of type char, so how do you insert data into this array and print it?
Typedef struct SequenceList {/ / element of the array char element [20]; / / length of the array int length;}
Define a structure in which there are two variables, one of which is an array pointer of type char, so how do you insert data into this array and print it?
/ / define the sequence table structure typedef struct SequenceList {char * elment; int length;}
The steps to deal with the structure here
Structure initialization
Structure in vivo data assignment
Structure output data in vivo
In accordance with the above principles, first operate on the first type
I. processing of char array types 1. Structure initialization SequenceList L; L.element = (char*) malloc (sizeof (char) * 10); L.length = 102. Structure in vivo data assignment (simple method) L.elment [0] = 1; L.elment [1] = 2; L.elment [2] = 3; L.elment [3] = 4; L.elment [4] = 5
For cycle
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]);} di. Char array pointer type processing 1. Structure initialization / / structure initialization MyList L; L.length = LENGTH; L.elment = (char*) malloc (L.length * sizeof (char)) 2. Structure body data assignment / / structure body assignment 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);}} 3. All code 1. Char array / / 010. Sequence table _ 004.cpp: this file contains the "main" function. Program execution will begin and end here. / / # include # define MAXSIZE 10 typedef struct SequenceList {/ / element of the array char element [MAXSIZE]; / / length of the array int length;}; int main () {/ / 1. Initialize the structure SequenceList* L; L = (SequenceList*) malloc (sizeof (char) * MAXSIZE); L-> length = MAXSIZE; / / 2. For (int I = 0; I) is stored in the structure.
< MAXSIZE; i++) { L->Element [I] ='a'+ I;} / / 3. Print the value for (int I = 0; I) inside the structure
< MAXSIZE; i++) { if (*(L->Element + I) > 0) {printf ("elment [% d] =% c\ n", I, * (L-> element + I));}
2. Char array pointer / / 011. Sequence table _ 005.cpp: this file contains the "main" function. Program execution will begin and end here. / / # include # define MAXSIZE 10typedef struct SequenceList {/ / element of the array char * element; / / the length of the array int length;}; int main () {/ / 1. Structure initialization SequenceList L; L.length = MAXSIZE; L.element = (char*) malloc (L.length * sizeof (MAXSIZE)); / / 2. Assign for (int I = 0; I) in the body of the structure
< 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));}
Thank you for reading this article carefully. I hope the article "how to assign char arrays in C language structure" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.