当前位置:网站首页>C declaration and initialization and assignment
C declaration and initialization and assignment
2022-07-29 04:00:00 【liyu5543】
c Declaration of data structure of language and initialization
In a word, summarize , In data type When making a statement , If you assign a value to this variable at the same time , This time is called initialization .
If at the time of declaration , No initialization is provided for variables . No matter what happens later, it cannot be called initialization , You can only assign values to this variable .
Typical are strings and data , structure .
Such as common data types
int a =3; // Declare variables a At the same time , Provide initialization operation .
int b;
b = 2; // This is called assignment , Not called initialization .
Such as arrays
int a[] = {1, 2, 3, 4}; // Declaration array a, And provide initialization .
int b[10];
b[1] = 233; This is called assignment .
Arrays and strings , There is something here
char a[] = {‘a’, ‘b’, ‘c’, ‘d’}; // Declaration character array a, Provide initialization , Notice the single quotation marks inside . As if c There are only three places in language that use single quotation marks , The first is char Define single character ;
char b[] = “hello world”; // Declaration character array b, Provide initialization . Notice that it looks like “hello world” Is a string constant , But in the expression initialized here , Put the “hello world” Break up , A character by character loading character array b.
char b[10]; // If initialization is not provided , Just specify the length , Whether explicit or implicit .
b[2] = ‘x’; // to b The second of assignment ; Another single quotation mark is used here . If it is a double quotation mark, it means two bits , By default, the last one is 、\0... Single quotation marks represent only one bit .
Here the array can only be operated one position at a time , Such as assignment , Replacement, etc . You cannot assign an array directly to another array , No matter what type the array is .
There are two ways to copy arrays , One is to use for Loop to assign values to each position . The second way is : use 《string.h》 Inside memcpy(a, b, sizeof(a));
For structures Is the same
struct {
int a;
char b;
} ss = {a=2, b=‘x’}; // Declare and initialize ; For this structure without structure label , This is the declared variable , There is no way to use this structure to declare variables later . Because there is no structural label .
struct p {
int c;
char *d;
} ss = {c = 12, d=“helllo”}; // Declare and initialize , This one has a structural label p The structure of the body , Subsequently, you can continue to define variables independently . If the structure is newly defined , Take it with you struct… They are together , Such as :
struct p s3; // Note that a type is defined here as struct p The structure of the body , The name is s3, But we didn't initialize , Only variables are defined
struct p s4 = {c = 33, d=“world”}; // Statement s4 Variable , Initialization is also provided . Notice here and the top variable ss … These two are the same to some extent , Both define new variables and initialize them at the same time .
meanwhile :
s3.c = 23;
s3.d = “new value”;
You can give it like this s3 The members in the structure are assigned .
Structures can be assigned to each other , This is better than arrays , An array cannot be assigned to another array , But the structure can .
So the top s3=s4 It's also OK Of .
At the same time, if you want to assign two arrays to each other , You can put two arrays into the structure first , Then assign values to the two structures , You can deform and realize the assignment of two arrays ;
Such as :
struct Arr {
int a[10];
} a1, a2;
Zeke a1=a2;
边栏推荐
- 【redis系列】字符串数据结构
- 【C语言入门】ZZULIOJ 1031-1035
- Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
- Basic configuration of BGP - establish peers and route announcements
- Malloc C language
- Since 2019, you must have stopped using this marketing strategy
- Overestimated test driven development?
- The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
- 无法一次粘贴多张图片
- UCOS task switching process
猜你喜欢

Since 2019, you must have stopped using this marketing strategy

Cloudera manager platform fault repair record

安装ros的laser_scan_matche库所遇到的问题(一)

消费行业数字化升级成 “刚需”,weiit 新零售 SaaS 为企业赋能!

Deep into C language (3) -- input and output stream of C

HCIP BGP

LDP -- label distribution protocol

数据挖掘——关联分析例题代码实现(下)
![[原理] 横向渗透的几种方式](/img/fc/2ef7dd6ebc5c0bd8f7d302d8b596d6.png)
[原理] 横向渗透的几种方式

向日葵远程控制为何采用BGP服务器?自动最优路线、跨运营商高速传输
随机推荐
1. Header file - Comment - namespace - standard input / output stream
代码 ~ 隐藏或禁用状态栏和虚拟按键
SQL语句 关于字段转换怎么写
SQL window function
Opensql quick learning
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac
How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency
How fast does it take to implement a super simple programming language?
Uni app internationalization
Configmap配置与Secret加密
"The programming is not standardized, and my colleagues are in tears!"
First knowledge of C language (3)
The list is not updated in real time when JS V-for data changes
C language to achieve three chess game (detailed explanation)
Form verification of landline
初识C语言(3)
LDP --- 标签分发协议
Several cases of word wrapping in div
Is the browser multi process or single process?