当前位置:网站首页>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;
边栏推荐
- How to write SQL statements about field conversion
- 路西法98-生活记录ing
- EMD empirical mode decomposition
- Alibaba Font Icon Library Usage and update methods
- Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds
- The const keyword of ES6 declares variables
- Form verification of landline
- tron OUT_ OF_ ENERGY
- HCIP BGP
- 【深度学习CPU(番外篇)——虚拟内存】
猜你喜欢

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘

How to understand clock cycle and formula CPU execution time = number of CPU clock cycles / dominant frequency

Why is continuous integration and deployment important in development?

Flutter 启动白屏

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

Solve the delay in opening the console of Google browser

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

EMD empirical mode decomposition
![[BGP] small scale experiment](/img/58/877e5e454e9bab9d1bccb8fdd3b04d.png)
[BGP] small scale experiment

EMD 经验模态分解
随机推荐
谁能详细说下mysqlRC下的半一致读和怎么样减少死锁概率?
Is the browser multi process or single process?
Ssl== certificate related concepts
C language to achieve three chess game (detailed explanation)
Common methods of lodash Library
Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac
安装ros的laser_scan_matche库所遇到的问题(一)
@Configuration (proxybeanmethods = false) what's the use of setting this to false
Some notes on uniapp
Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
Analysis of new retail o2o e-commerce model
SSL==证书相关概念
Alibaba Font Icon Library Usage and update methods
SQL窗口函数
Data too long for column 'xxx' at row 1 solution
Solve the delay in opening the console of Google browser
Basic configuration of BGP - establish peers and route announcements
What have I learned from 200 machine learning tools?
Wechat applet monitors sliding events on the screen
OA项目之会议通知(查询&是否参会&反馈详情)