当前位置:网站首页>[learning notes, dog learning C] deeply understand arrays and pointers
[learning notes, dog learning C] deeply understand arrays and pointers
2022-07-22 17:29:00 【Jiang Junzhu】
List of articles
One 、 Pre reminder
sizeof() It's a monocular operator , Used to calculate the length of data , In bytes
strlen yes C A function defined by the language standard library , Used to calculate the length of a string , Until the empty end character , But does not include the null end character . Looking at the source code, we can see ,strlen To receive a pointer , That is to receive an address .
The array name is the address of the first element of the array , But there are 2 Exceptions :1.sizeof( Array name ), Here, the array name represents the whole array ;2. & Array name , Here, the array name represents the whole array , It takes out the address of the entire array .
Two 、 One dimensional array
First calculate the print result , Then look at the answers behind , written examination !!! Do not copy directly to the compiler .
- Shape array
int main() {
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a + 0));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(a[1]));
printf("%d\n", sizeof(&a));
printf("%d\n", sizeof(*&a));
printf("%d\n", sizeof(&a + 1));
printf("%d\n", sizeof(&a[0]));
printf("%d\n", sizeof(&a[0] + 1));
return 0;
}
int main() {
// Here are 32 Calculation results in the case of bit machine
int a[] = {
1,2,3,4 };
printf("%d\n", sizeof(a));
// One int Type account 4 Bytes ,4 individual int type , The result is 16
printf("%d\n", sizeof(a + 0));
//a As an array name, it does not appear alone , So here a Represents the address of the first element
// First element address +0 after , Or the first element address
// The size of an address is 4
printf("%d\n", sizeof(*a));
//a Represents the address of the first element ,*a Is to dereference the address of the first element , Get integer 1
// An integer size is 4 Bytes
printf("%d\n", sizeof(a + 1));
//a + 1 What you get is the address of the second element
// The size of an address is 4
printf("%d\n", sizeof(a[1]));
//a[1] What you get is the second element
//int The type size is 4 Bytes
printf("%d\n", sizeof(&a));
// Get the address of the entire array , The address of the array is also the address
// The size of an address is 4
printf("%d\n", sizeof(*&a));
//&a What you get is the address of the whole element , Dereference and get the whole array
//4 individual int type , The result is 16
printf("%d\n", sizeof(&a + 1));
//&a Get an address , Address +1 After that, I got an address
// The size of an address is 4
printf("%d\n", sizeof(&a[0]));
// The operator [] The priority ratio & high
// First a[0], Represents the first element of the array
// Then take the address &, Get the address of the first element
printf("%d\n", sizeof(&a[0] + 1));
// At the address of the first element +1, Get the address of the second element
return 0;
}
- A character array
int main() {
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
int main() {
// Here are 32 Calculation results in the case of bit machine
char arr[] = {
'a','b','c','d','e','f' };
printf("%d\n", sizeof(arr));
// One char The type size is 1 byte ,6 individual char type , Print 6
printf("%d\n", sizeof(arr + 0));
//arr As an array name, it does not appear alone , So here arr Represents the address of the first element
// First element address +0 after , Or the first element address
// The size of an address is 4 byte
printf("%d\n", sizeof(*arr));
//arr Represents the address of the first element , Get the first element after dereferencing
// One char The type size is 1 byte
printf("%d\n", sizeof(arr[1]));
// For the second element , One char The type size is 1 byte
printf("%d\n", sizeof(&arr));
// Get the address of the entire array , The size of an address is 4 byte
printf("%d\n", sizeof(&arr + 1));
// Get the address of the entire array , Then the address +1, Get another address
printf("%d\n", sizeof(&arr[0] + 1));
// Get the address of the first element +1, Get the address of the second element
// Be careful : Here is strlen, No sizeof 了 , Do not continue to use sizeof count (doge)
printf("%d\n", strlen(arr));
//strlen find ‘\0’ Just stop ,
// however arr There's no storage ‘\0’
// that strlen I'll keep looking for it , Until you find one ‘\0’ until
// So it prints out as a random value
printf("%d\n", strlen(arr + 0));
//arr+0 Get the first element ,
// Then calculate from the first element , It turns out that there is still no ‘\0’
// So the compiler silently prints a random value
printf("%d\n", strlen(*arr));
//*arr Dereference to get characters ‘a’,
// The problem arises ,strlen Only receive addresses
// So this line of code is wrong
printf("%d\n", strlen(arr[1]));
//arr[1] Get the characters ‘b’, Report errors
printf("%d\n", strlen(&arr));
//&arr Get the address of the entire array ,
//BUT This array has no ‘\0’,
// So the compiler silently prints a random value
printf("%d\n", strlen(&arr + 1));
//&arr + 1 Get an address
//BUT The devil knows ‘\0’ Where will it appear
// So the compiler silently prints a random value
printf("%d\n", strlen(&arr[0] + 1));
// After getting the address of the first element +1, Get the second element
//BUT The second element is unknown ‘\0’ Where will it appear
// So the compiler silently prints a random value
return 0;
}
int main() {
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
printf("%d\n", sizeof(arr + 0));
printf("%d\n", sizeof(*arr));
printf("%d\n", sizeof(arr[1]));
printf("%d\n", sizeof(&arr));
printf("%d\n", sizeof(&arr + 1));
printf("%d\n", sizeof(&arr[0] + 1));
printf("%d\n", strlen(arr));
printf("%d\n", strlen(arr + 0));
printf("%d\n", strlen(*arr));
printf("%d\n", strlen(arr[1]));
printf("%d\n", strlen(&arr));
printf("%d\n", strlen(&arr + 1));
printf("%d\n", strlen(&arr[0] + 1));
return 0;
}
int main() {
// Here are 32 Calculation results in the case of bit machine
char arr[] = "abcdef";
printf("%d\n", sizeof(arr));
// Strings are stored with ‘\0’
//sizeof No strlen, It will also have a ‘\0’
// therefore 7 individual char type , Print 7
printf("%d\n", sizeof(arr + 0));
//arr As an array name, it does not appear alone , So here arr Represents the address of the first element
// First element address +0 after , Or the first element address
// The size of an address is 4 byte
printf("%d\n", sizeof(*arr));
// here arr Represents the address of the first element , The first element after dereference , One char The type size is 1
printf("%d\n", sizeof(arr[1]));// For the second element , One char The type size is 1
printf("%d\n", sizeof(&arr));// The address of the entire array , The size of an address is 4
printf("%d\n", sizeof(&arr + 1));// Address +1 Get the address , The size of an address is 4
printf("%d\n", sizeof(&arr[0] + 1));// Get the address of the second element , The size of an address is 4
printf("%d\n", strlen(arr));
// Strings are stored with ‘\0’
//strlen encounter ‘\0’ Just stop , And do not calculate ‘\0’
// So print 6
printf("%d\n", strlen(arr + 0));
// Get the address of the first character , Start calculating the length , encounter ‘\0’ Just stop , Print 6
printf("%d\n", strlen(*arr));//strlen To receive address , Report errors
printf("%d\n", strlen(arr[1]));//strlen To receive address , Report errors
printf("%d\n", strlen(&arr));// Take the address of the whole array , Print 6
printf("%d\n", strlen(&arr + 1));
// Take the address of the whole array ,+1 Skip this array
// So I don't know where there is ‘\0’ 了
// Print a random value
printf("%d\n", strlen(&arr[0] + 1));// Fetch ‘b’ The address of , Start calculating , The result is 5
return 0;
}
3、 ... and 、 Two dimensional array
int main() {
// Two dimensional array
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));
printf("%d\n", sizeof(a[0][0]));
printf("%d\n", sizeof(a[0]));
printf("%d\n", sizeof(a[0] + 1));
printf("%d\n", sizeof(*(a[0] + 1)));
printf("%d\n", sizeof(a + 1));
printf("%d\n", sizeof(*(a + 1)));
printf("%d\n", sizeof(&a[0] + 1));
printf("%d\n", sizeof(*(&a[0] + 1)));
printf("%d\n", sizeof(*a));
printf("%d\n", sizeof(a[3]));
return 0;
}
int main() {
// Two dimensional array
int a[3][4] = {
0 };
printf("%d\n", sizeof(a));
// Two dimensional array with three rows and four columns , All are int type
//3 * 4 * 4 have to 48
printf("%d\n", sizeof(a[0][0]));// The first element in the first line , Print 4
printf("%d\n", sizeof(a[0]));
//a[0] As an array, the list is placed alone in sizeof Inside , The first line
// One line is stored 4 individual int Type element , have to 16
printf("%d\n", sizeof(a[0] + 1));
//a[0] Not alone in sizeof Inside , No address , Represents the address of the first element in the first line ,
//+1 Get the address of the second element in the first line , The size of an address is 4
printf("%d\n", sizeof(*(a[0] + 1)));// Dereference to get the second element in the first line , One int The type size is 4
printf("%d\n", sizeof(a + 1));
// Represents the address of the first element , That's the first row
//+1 Get the second line of address , The size of an address is 4
printf("%d\n", sizeof(*(a + 1)));// Dereference to get the second row array ,4 individual int The type size is 16
printf("%d\n", sizeof(&a[0] + 1));//&a[0] Get the first line address ,&a[0] + 1 Get the second line of address ,4
printf("%d\n", sizeof(*(&a[0] + 1)));// Dereference the second line , Get the second row array ,4 individual int The type size is 16
printf("%d\n", sizeof(*a));//a Represents the address of the first element , The first row array of dereference ,4 individual int The type size is 16
printf("%d\n", sizeof(a[3]));
// If it exists ,a[3] Represents the array name of the fourth row ,
//sizeof It doesn't need to exist , Calculate the type ,4 individual int The type size is 16
return 0;
}
Four 、 The pointer
int main() {
char* p = "abcdef";
printf("%d\n", sizeof(p));
printf("%d\n", sizeof(p + 1));
printf("%d\n", sizeof(*p));
printf("%d\n", sizeof(p[0]));
printf("%d\n", sizeof(&p));
printf("%d\n", sizeof(&p + 1));
printf("%d\n", sizeof(&p[0] + 1));
printf("%d\n", strlen(p));
printf("%d\n", strlen(p + 1));
printf("%d\n", strlen(*p));
printf("%d\n", strlen(p[0]));
printf("%d\n", strlen(&p));
printf("%d\n", strlen(&p + 1));
printf("%d\n", strlen(&p[0] + 1));
return 0;
}
int main() {
char* p = "abcdef";
printf("%d\n", sizeof(p));//p It's a pointer , The address , The size of an address is 4
printf("%d\n", sizeof(p + 1));// Address +1 It's still an address , The size is 4
printf("%d\n", sizeof(*p));
// The pointer P It stores the first element ‘a’ The address of
// Dereference to get the element ‘a’
// One char The size of the type is 1
printf("%d\n", sizeof(p[0]));//p[0] Quite so p To dereference , Get the element ‘a’, The size is 1
printf("%d\n", sizeof(&p));
// Pointer is also a variable , He will also open up an address to store
// take p The address of is still an address , The size is 4
printf("%d\n", sizeof(&p + 1));// Address +1 It's still an address , The size is 4
printf("%d\n", sizeof(&p[0] + 1));
// obtain ‘a’ After the address of
// Right again ‘a’ Take the address to get an address
// The size of an address is 4
printf("%d\n", strlen(p));//p Deposit is ‘a’ The address of , from ‘a’ Start calculating , The result is 6
printf("%d\n", strlen(p + 1));//p+1 Get back ‘b’ The address of , from ‘b’ Start calculating , The result is 5
printf("%d\n", strlen(*p));//p After dereferencing, we get ‘a’,strlen To receive an address , Report errors
printf("%d\n", strlen(p[0]));//p[0] Quite so p To dereference , Get the element ‘a’, Report errors
printf("%d\n", strlen(&p));
// Get pointer p The address of , from p Start to calculate the address of
// But I don't know where there will be ‘\0’, So print out a random value
printf("%d\n", strlen(&p + 1));// Random value
printf("%d\n", strlen(&p[0] + 1));
//p[0] Quite so p To dereference , Get the element ‘a’
// Right again ‘a’ Address fetch , obtain ‘a’ The address of ,+1 obtain ‘b’ The address of
// from ‘b’ Start calculating , The result is 5
return 0;
}
Is this ? Is this ? Is this ? Isn't it about two handfuls of hair ?
边栏推荐
- Sql语言(基础一)
- 什么是NumPy?
- MVC模式和三层架构
- Causes of server buffer/cache and buffer/cache release
- 【外部排序】归并思想完成外部排序
- Openeuler is ambitious, open source huizhichuang future | 2022 open atom global open source summit openeuler sub forum is about to open
- 对称式加密与非对称式加密的对比
- Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
- ssrf漏洞攻击内网Redis复现
- The three formats of the log "binlog" in MySQL are so interesting
猜你喜欢
How to solve the problem of uncontrollable win11 flashing white screen?
Win11遇到问题需要重启怎么办?
分布式计算框架Map/Reduce
UART通信实验(查询方式)
This article introduces you to the workflow of Redux - action/reducer/store
Polygon chain matic concept and underlying mechanism
Pytorch optimizer: optim SGD && optimizer.zero_ grad()
【矩阵乘法】外部矩阵乘法
What is numpy?
以CRM系统为案例讲解数据分析(重要性介绍及分析方法)
随机推荐
Is it safe to open an account with the VIP link of Galaxy Securities? What is the lowest commission?
What is I18N and what is its function
Go language learning diary [XXXI] interaction between golang and PgSQL
ACM warm-up exercise 3 in 2022 summer vacation (summary of some topics)
MVC mode and three-tier architecture
NFTFi赛道版图概览
How to implement Apache's built-in AB stress testing tool
《PyTorch深度学习实践》-B站 刘二大人-day2
[C language interesting experiment]
How to solve the problem of uncontrollable win11 flashing white screen?
Breadth first search
服務器buffer/cache 的產生原因和釋放buffer/cache
牛客网 替换空格
C语言分支结构和循环结构(1)
mysql约束之_主键约束PRIMIARY KEY
mysql约束之_唯一约束
FPGA之红外遥控
Web3流量聚合平台Starfish OS,给玩家元宇宙新范式体验
MySQL constraint_ Self growth constraint_ auto_ increment
Go语言学习日记【三十一】golang与pgsql交互