当前位置:网站首页>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;
边栏推荐
- nacos注册中心
- Vs code must know and know 20 shortcut keys!
- Process tracking of ribbon principle
- Extended operator of new features in ES6
- Why do many programmers hate pair programming?
- Data too long for column 'xxx' at row 1 solution
- Deconstruction assignment of new features of ES6
- SQL窗口函数
- Cannot paste multiple pictures at once
- Spark dataframe replaces empty characters (or other values) in each column with null
猜你喜欢

Object array merges elements according to a field

Connection broken by 'readtimc rt-443): read timed out (read timeout=l5)“)‘: /pac

Flutter 启动白屏

Microcomputer principle and interface technology

Shopify卖家:EDM营销就要搭配SaleSmartly,轻松搞定转化率

Configmap configuration and secret encryption

Process tracking of ribbon principle

Simple cases of inner connection and left connection

Solve the problem of garbled code when opening the project code in idea

UCOS任务切换过程
随机推荐
路西法98-生活记录ing
[principle] several ways of horizontal penetration
Data too long for column 'xxx' at row 1 solution
LVS+KeepAlived高可用部署实战应用
Ssl== certificate related concepts
Object array merges elements according to a field
Typescript from getting started to mastering (XXIII) namespace namespace (Part 2)
JS realizes the function of one click Copy
LDP -- label distribution protocol
Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
Cannot paste multiple pictures at once
Typescript from getting started to mastering (19) enumeration types
【C语言入门】ZZULIOJ 1031-1035
Ribbon principle analysis namedcontextfactory
1. Header file - Comment - namespace - standard input / output stream
About the writing of ALV format control part
First ALV program 2
数据挖掘——关联分析基础介绍(上)
通过PWM呼吸灯和PWM控制直流电机来详细介绍TIM的输出比较功能
What you see and think in Microsoft