当前位置:网站首页>Summary of basic notes of C language (2)
Summary of basic notes of C language (2)
2022-06-24 05:51:00 【Distant stars】
- C Language learning notes , Record what you learn , Easy to review . Because of the length is too large , Considering the look and feel , Prepare multiple records .
- Learn video links :《 Learn from you C Take you fly 》
- IDE:Dev-C++ 5.11
- To practice programming habits , Temporarily abandon Clion
- Front row reminders : Recommended collection
Series links
C Summary of basic notes of language 10000 words ( One )
1、 Definition of array
- Format : type Number of array name elements
Such as :int i5
2、 Accessing array elements
- Format : Array name subscript Such as : i0; // visit i No 1 Elements i3; // visit i No 4 Elements
notes : The subscript of an array is from 0 At the beginning , Subscript 0 Corresponds to the first element of the array
3、 Initialization of an array
- Initializes all elements in the array to 0
int a[5] = {0};
// in fact , This just assigns the first element to 0, The rest of you are automatically initialized to 0
int a[5] = {1};
// Here is the , The first element is 1, The rest of the elements are 0- If you give tuples different values , Separate them with commas
int a[5] = {1, 2, 3, 4, 5};- You can also assign values to only some elements , Unassigned elements , Auto initialize to 0
int a[5] = {1, 2, 3};
// The first three successful assignments , The last two , Auto initialize to 0- You can also specify elements for assignment , Unassigned elements are automatically initialized to 0
int a[5] = {[2] = 2, [4] = [4]};
// Array a Of the 3 The bit is assigned to 2, The first 5 The bit is assigned to 4, Because the subscript of an array is from 0 At the beginning !!!- You can also give only the value of the element , Do not specify the length of the array
( The compiler will automatically determine the length of the array according to the number of values )
int a[] = {1, 2, 3, 4, 5}Two 、 Two dimensional array
1、 Definition of 2D array
- Format : type Array name constant expression
2、 Access to two-dimensional arrays
- Format : Array name subscript
Such as :
a0; // visit a The elements in the first row and the first column of the array
b2; // visit b Elements in the third row and fourth column of the array
3、 Initialization of 2D array
- Because the two-dimensional array is stored linearly in memory , So you can write all the data in a curly bracket
int a[2][3] = {1, 2, 3, 4, 5, 6};How on earth is it stored , Print it with a program , Observations .
#include <stdio.h>
int main()
{
int a[2][3] = {1, 2, 3, 4, 5, 6};
int i, j;
for (i = 0;i < 2;i++)
{
for (j = 0;j < 3;j++)
{
printf("a[%d][%d] = %d\n", i, j, a[i][j]);
}
}
return 0;
}give the result as follows :
a[0][0] = 1 a[0][1] = 2 a[0][2] = 3 a[1][0] = 4 a[1][1] = 5 a[1][2] = 6
Pass the result , Is it easy to see ~
- More intuitive representation of the distribution of elements , You can enclose the elements of each line with braces
int a[2][3] = {{1, 2, 3}, { 4, 5, 6}};Or write in this style :
int a[][3] = {
{1, 2, 3},
{4, 5, 6}
};The number of lines in the above two forms can be omitted without writing
- Initialize the entire two-dimensional array to 0, You just need to write a... In braces 0
int a[2][3] = {0};- Initializes some specified elements , Other elements are automatically initialized to 0
int a[2][3] = {[0][0] = 1, [1][2] = 2};3、 ... and 、 The pointer
1、 Defining pointer variables
- Format : Type name * Pointer variable name
char *pa; // Define a pointer variable that points to a character type
int *pb; // Define a pointer variable that points to an integer
Take address operator and value operator
- If you need to get the address of a variable , You can use the address operator (&) Such as :char *pa = &a;
- If you need to access the data pointed to by the pointer variable , You can use the value operator (*) Such as :printf("%c, %d\n", pa, pb);
Be careful :
It is worth mentioning that * This symbol , You need it when you define a pointer , At the same time, it is the value operator . It needs to be understood slowly
== Avoid using uninitialized pointers ==
int *p // For example, this is an uninitialized pointer ----------------------------- int *p = &a // For example, this is the initialized pointer
# include <stdio.h>
int main(){
char a = 'H';
int b = 521;
char *pa = &a;
int *pb = &b; // there * Is used to define pointer variables
printf("a=%c\n", *pa); // What is printed here is the variable value pointed to by the address
printf("b=%d\n", *pb); // there * Is used to get the value
printf("sizeof pa = %d\n", sizeof(pa));
printf("sizeof pb = %d\n", sizeof(pb));
printf("a The address of :%p\n", pa); // Here is the address where the pointer is stored
printf("b The address of :%p\n", pb);
return 0;
}result :
a=H b=521 sizeof pa = 8 sizeof pb = 8 a The address of :000000000062FE0F b The address of :000000000062FE08
== The array name is actually the address of the first element of the array ==
# include <stdio.h>
int main(){
char str[3];
printf(" Please enter the most handsome blogger :");
scanf("%s", str);
printf(" The address of the most handsome blogger is :%p\n", str); // View the address of the array name
printf(" The address of the most handsome blogger is :%p\n", &str[0]); // Get the address of the first element of the array
return 0;
}Running results :
Please enter the most handsome blogger : Stars in the distance The address of the most handsome blogger is :000000000062FE10 The address of the most handsome blogger is :000000000062FE10
What is the relationship between the address of other positions of the array and the first address ?
# include <stdio.h>
int main(){
char a[] = "Hello";
int b[] = {1, 2, 3, 4, 5};
float c [] = {1.1, 2.2, 3.3, 4.4};
double d[] = {1.1, 2.2, 3.3, 4.4};
printf("a[0] -> %p, a[1] -> %p, a[2] -> %p\n", &a[0], &a[1], &a[2]);
printf("b[0] -> %p, b[1] -> %p, b[2] -> %p\n", &b[0], &b[1], &b[2]);
printf("c[0] -> %p, c[1] -> %p, c[2] -> %p\n", &c[0], &c[1], &c[2]);
printf("d[0] -> %p, d[1] -> %p, d[2] -> %p\n", &d[0], &d[1], &d[2]);
return 0;
}Running results :
a[0] -> 000000000062FE10, a[1] -> 000000000062FE11, a[2] -> 000000000062FE12 b[0] -> 000000000062FDF0, b[1] -> 000000000062FDF4, b[2] -> 000000000062FDF8 c[0] -> 000000000062FDE0, c[1] -> 000000000062FDE4, c[2] -> 000000000062FDE8 d[0] -> 000000000062FDC0, d[1] -> 000000000062FDC8, d[2] -> 000000000062FDD0
This is hexadecimal , Array a,char type , In turn increase 1 Bytes ; Array b,int type , In turn increase 4 Bytes ; Array c,float type , In turn increase 4 Bytes ; Array d,double type , In turn increase 8 Bytes . Opposite to the following table , It's not hard to find the law , The increasing size is the number of bytes occupied by the data type .
char | 1 Bytes |
|---|---|
char* | 8 Bytes |
short int | 2 Bytes |
int | 4 Bytes |
unsigned int | 4 Bytes |
float | 4 Bytes |
double | 8 Bytes |
long | 8 Bytes |
long long | 8 Bytes |
unsigned long | 8 Bytes |
2、 The operation of the pointer
When the pointer points to an array element , We can add and subtract pointer variables , The meaning of this is equivalent to pointing forward or backward... From the position of the pointer n Elements .
- example :
# include <stdio.h>
int main(){
char a[] = "Hello";
char *p = a;
printf("*p = %c, *(p+1) = %c, *(p+3) = %c\n", *p, *(p+1), *(p+3));
return 0;
}result :
*p = H, *(p+1) = e, *(p+3) = l
thus it can be seen , Addition and subtraction represent the direction .p+1 refer to , Point to the next element of the array , that p-1, Is to point to the previous element of the array .
- Here, the method of using pointers to indirectly access array elements is called pointer method .
3、 The difference between pointers and arrays
- The array name is just an address , And the pointer is an lvalue .
lvalue( The left value ):C In language, an identifier used to identify or locate a storage location , Lvalue must be changeable .
4、 Pointer array
- for example :
int *p[5]
- An array of pointers is an array , Each array element holds a pointer variable .
Very easy to use when pointing to a character pointer .
# include <stdio.h>
int main(){
char *p[] = {
" If you choose, you don't have to be afraid ",
" If you work hard, you will succeed ",
" come on. , A stranger !"
};
int i;
for (i = 0;i < 3;i++){
printf("%s\n", p[i]); // It can't be written here *p[i] Of , This means that it points to the address of the first element of the array
}
return 0;
}5、 Array pointer
- for example :
int (*p)[5]
- Array pointer is a pointer , It points to an array
example : This is done using array pointers
# include <stdio.h>
int main(){
int temp[5] = {1, 2, 3, 4, 5};
int (*p2)[5] = &temp; // Pointer to array
int i;
for (i = 0;i < 5;i++)
{
printf("%d\n", *(*p2 + i));
}
return 0;
}Running results :
1 2 3 4 5
- Compare : Use ( Ordinary ) The pointer ==( The array name is actually the address of the first element of the array )== Although the results are equal , But it's different in essence ...# include <stdio.h> int main(){ int temp[5] = {1, 2, 3, 4, 5}; int *p = temp; // Pointer to the first element of the array int i; for (i = 0;i < 5;i++) { printf("%d\n", *(p + i)); } return 0; }
6、 Pointers and two-dimensional arrays
arry It points to the inclusion of 5 A pointer to an array of elements
Take the value of an address , be called ==“ Quoting ”==
What exactly is dereference , I checked some information , Is it really “ Take the content of the address pointed to by the pointer ” It's not known , It also needs to precipitate slowly in the future .
Quoting :
*(array+i) == array[i] *(*(array+i)+j) == array[i][j] *(*(*(array+i)+j)+k) == array[i][j][k]
The form of subscript index can be converted to the form of indirect index using pointer .
- example :
# include <stdio.h>
int main(){
int array[4][5] = {0};
int i,j,k = 0;
for (i=0;i<4;i++)
{
for(j=0;j<5;j++)
{
array[i][j] = k++;
}
}
printf("*(array+1):%p\n", *(array + 1));//array+1 And dereference
printf("array[1]:%p\n", array[1]);//array The address at the beginning of the second line
printf("&array[1][0]:%p\n", &array[1][0]);// Yes array[1][0] Address fetch
printf("**(array+1):%d\n", **(array+1));// Yes array+1 Dereference of and
printf("array[1][0]:%d\n", array[1][0]);//array[1][0] Value
return 0;
}Running results :
*(array+1):000000000062FDC4 array[1]:000000000062FDC4 &array[1][0]:000000000062FDC4 **(array+1):5 array[1][0]:5
7、void The pointer
- void Pointer is also called general pointer , It can point to any type of data . Any type of pointer can be assigned to void The pointer .
- example : Check it out :
# include <stdio.h>
int main(){
int num = 521;
int *p1 = #
char *p2 = " God helps those who help themselves ";
void *p3;
p3 = p1;
printf("p1:%p, p3:%p\n", p1, p3);
p3 = p2;
printf("p2:%p, p3:%p\n", p2, p3);
return 0;
}give the result as follows :
p1:000000000062FE04, p3:000000000062FE04 p2:0000000000404000, p3:0000000000404000
The result is the same , thus it can be seen , Can assign a pointer of any data type to void The pointer to .
== But if it's based on void The pointer , Take out its contents , It's best to cast data types .==
- Modify according to the above example :
# include <stdio.h>
int main(){
int num = 521;
int *p1 = #
char *p2 = " God helps those who help themselves ";
void *p3;
p3 = p1;
printf("p3:%d\n", *(int *)p3);//(int *)p3 Is to cast types , Add one *, To dereference , To get the value
p3 = p2;
printf("p3:%s\n", (char *)p3);// Due to the particularity of string , You don't need to reference
return 0;
}give the result as follows :
p3:521 p3: God helps those who help themselves
8、NULL The pointer
NULL Pointer is also called null pointer , What does it do ?
- It is conducive to develop good programming habits When it is not clear which address to initialize the pointer to , You can initialize it to NULL; When dereferencing a pointer , First check whether the pointer is NULL.== Can effectively avoid wild pointer ==
9、 The pointer to the pointer
- benefits :
① Avoid duplicate memory allocation
② Only one change is needed
10、 pointer to const
I've seen constants defined with macro definitions before , In fact, we can use const keyword .
- const Decorated data type refers to constant type , The value of a constant type variable or object cannot be updated , That is, it can only be read and cannot be rewritten .
example :
# include <stdio.h>
int main(){
const float pi = 3.14;
printf("%f\n",pi);
pi = 3.1415;
return 0;
}After the compilation , Will report an error directly !
[Error] assignment of read-only variable 'pi' // [ error ] A read-only variable 'pi' Assignment
From this, we can get a small conclusion about pointers to constants :
pointer to const
- Pointers can be modified to point to different constants
- Pointers can be modified to point to different variables
- You can use dereference to read the data pointed to by the pointer
- The data pointed to by the pointer cannot be modified by dereference
11、 Constant pointer
①、 A constant pointer to a non constant quantity
- The pointer itself cannot be modified
- The value pointed to by the pointer can be modified
for example :
# include <stdio.h>
int main(){
int num = 521;
const int cnum = 1024;
int * const p = #
*p = 7758;// Modify the value that the pointer points to
printf("*p:%d\n", *p);
return 0;
}Output is :
*p:7758
Modify the above example :
# include <stdio.h>
int main(){
int num = 521;
const int cnum = 1024;
int * const p = #
p = &cnum;// Modify the pointer itself
printf("*p:%d\n", *p);
return 0;
}Compilation result , The system will report an error !
[Error] assignment of read-only variable 'p'
②、 Constant pointer to constant
- The pointer itself cannot be modified
- The value pointed to by the pointer can also be unmodified
( Similar to the above example )
Four 、 To be continued
The pointer is really C The soul of , More haste, less speed. , It takes a long time , Keep practicing to better understand .
The notes of this stage will be recorded here first , But learn C There is still a long way to go ...
What a long long road! , I will go up and down .
Come on, everyone !
author : Stars in the distance CSDN:https://blog.csdn.net/qq_44921056
This article is only for communication learning , Without the permission of the author , Prohibited reproduced , Let alone for other purposes , Offenders will investigate .
边栏推荐
- How to get a secondary domain name? What does a secondary domain name mean?
- Go's package management learning notes
- Tencent cloud harbor private warehouse deployment practice
- Figure 1 understand Tencent reassurance platform
- 2021, how to select a programming language?
- PNAs: development of white matter pathways in human brain during the second and third trimester of pregnancy
- How to register domain name and web address? What is the domain name and URL?
- Talk about my working experience in Tencent and byte
- Fixed assets management software enables enterprises to realize intelligent management of fixed assets
- What is the domain name system? What are the effects of domain names on Enterprises
猜你喜欢
随机推荐
How to set the secondary domain name of the website? What should I pay attention to when setting the domain name?
Talk about my working experience in Tencent and byte
The instrument industry builds the supplier SRM mode to manage the development of manufacturers + users
How to buy a network domain name? Is the domain expensive
How to build a website with a domain name? Is the website domain name free to use?
My two-year persistence is worth it!
Supply chain innovation of industrial Internet cloud computing
How to build a website with a domain name? What steps need to be taken?
How to apply for a website domain name and what problems should be paid attention to
What is the learning path for model deployment optimization?
How about the XYZ domain name? What are the advantages over other domain names?
Distributed background task load balancing
How to make a secondary domain name? What are the advantages of secondary domain names?
When we talk about zero trust, what are we talking about?
Confirm the importance of requirements at the initial stage of EDI project
[JS reverse hundred examples] Dangle login interface parameters reverse
What domain name is top? What are the advantages of the top domain name?
Experience sharing on unified management and construction of virtual machine
What is a website domain name and why do you want to register a domain name
Mysql database backup under Windows Environment