当前位置:网站首页>C language array and pointer exercises
C language array and pointer exercises
2022-06-11 03:07:00 【shlyyy】
C Language array and pointer exercises
Preface
Understanding C Language pointer Based on this article , Do the following exercises to better master the knowledge of arrays and pointers .
7、 ... and 、 practice
The array name indicates the address of the first element of the array
But there are 2 Exceptions :
- sizeof( Array name ), The array name represents the entire array , The total size of the array is calculated , Unit is byte .
- & Array name , The array name represents the entire array ,& Array name It takes out the address of the entire array
besides , All array names encountered represent the address of the first element of the array .
*(arr + n) = arr[n]
7.1 One dimensional array
void test1()
{
int a[] = {
1,2,3,4 };
printf("%d\r\n", sizeof(a));
printf("%d\r\n", sizeof(a + 0));
printf("%d\r\n", sizeof(*a));
printf("%d\r\n", sizeof(a + 1));
printf("%d\r\n", sizeof(a[1]));
printf("%d\r\n", sizeof(&a));
printf("%d\r\n", sizeof(*&a));
printf("%d\r\n", sizeof(&a + 1));
printf("%d\r\n", sizeof(&a[0]));
printf("%d\r\n", sizeof(&a[0] + 1));
}
void test1()
{
int a[] = {
1,2,3,4 };
// sizeof( Array name ) The total size of the array is calculated
printf("%d\r\n", sizeof(a)); // 16
// Array name a Is the address of the first element of the array ,32 The address of the bit program is 4 byte
printf("%d\r\n", sizeof(a + 0)); // 4
// Array name a Is the address of the first element of the array ,*a Get the first element of the array 1, The type is int size 4 byte
// *(arr + n) = arr[n] therefore *a=*(a+0)=a[0]
printf("%d\r\n", sizeof(*a)); // 4
// Array name a Is the address of the first element of the array ,a+1 Get the address of the next element , The address is 4 Byte size
printf("%d\r\n", sizeof(a + 1)); // 4
// The array element type is int size 4 byte
printf("%d\r\n", sizeof(a[1])); // 4
// & The array name represents the address of the entire array , The address is 4 Byte size
printf("%d\r\n", sizeof(&a)); // 4
// &a For the address of the entire array , Solve the star to get the whole array , amount to * And & Offset , The array size is 16 byte
printf("%d\r\n", sizeof(*&a)); // 16
// &a For the address of the entire array , Add 1 Get past the array a Address after , Address size 4 byte
printf("%d\r\n", sizeof(&a + 1)); // 4
// Address of the first element of the array , Address size 4 byte
printf("%d\r\n", sizeof(&a[0])); // 4
// Address of the first element of the array , The type is int*, Add 1 Represents the address of the next element , Address size 4 byte
printf("%d\r\n", sizeof(&a[0] + 1)); // 4
}
7.2 A character array 1
void test2()
{
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));
}
Reference resources :
void test2()
{
char arr[] = {
'a','b','c','d','e','f' };
// sizeof( Array name ) Calculate the size of the entire array
printf("%d\n", sizeof(arr)); // 6
// Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array , Address 4 Byte size
printf("%d\n", sizeof(arr + 0)); // 4
// arr Represents the address of the first element of the array ,*arr Get the first element of the array a, The type is char size 1 byte
printf("%d\n", sizeof(*arr)); // 1
// The array index is zero 1 The element of is b, The type is char size 1 byte
printf("%d\n", sizeof(arr[1])); // 1
// &arr For the address of the entire array , Address 4 Byte size
printf("%d\n", sizeof(&arr)); // 4
// &arr For the address of the entire array , Add 1 Get the address across the entire array , Address size 4 byte
printf("%d\n", sizeof(&arr + 1)); // 4
// Address of the first element of the array , Add 1 Get the address of the next element , Address size 4 byte
printf("%d\n", sizeof(&arr[0] + 1)); // 4
// arr Address of the first element of the array , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(arr)); // random number , Test for 19
// Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(arr + 0)); // random number , Test for 19
// Array name arr Represents the address of the first element of the array ,*arr Get the first element a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
printf("%d\n", strlen(*arr));
// ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
printf("%d\n", strlen(arr[1]));
// &arr Get the address of the entire array , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&arr)); // random number , Test for 19
// &arr Get the address of the entire array , Add 1 Get the address across the entire array , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&arr + 1)); // random number , Test for 13
// Address of the first element of the array , Add 1 Get the address of the next element , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&arr[0] + 1)); // random number , Test for 18
}
7.3 A character array 2
void test3()
{
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));
}
Reference resources :
void test3()
{
char arr[] = "abcdef"; // The string is in the stack area , It automatically includes '\0'
// sizeof( Array name ) Calculate the size of the entire array
printf("%d\n", sizeof(arr)); // 7
// The array name is the address of the first element of the array , Add 0 Or the address of the first element , Address size 4 byte
printf("%d\n", sizeof(arr + 0)); // 4
// arr Represents the address of the first element of the array ,*arr Get the first element of the array a, The type is char size 1 byte
printf("%d\n", sizeof(*arr)); // 1
// The array index is zero 1 The element of is b, The type is char size 1 byte
printf("%d\n", sizeof(arr[1])); // 1
// &arr For the address of the entire array , Address 4 Byte size
printf("%d\n", sizeof(&arr)); // 4
// &arr For the address of the entire array , Add 1 Get the address across the entire array , Address size 4 byte
printf("%d\n", sizeof(&arr + 1)); // 4
// Address of the first element of the array , Add 1 Get the address of the next element , Address size 4 byte
printf("%d\n", sizeof(&arr[0] + 1)); // 4
// arr Address of the first element of the array ,strlen encounter '\0' end , The resulting length does not include '\0'
printf("%d\n", strlen(arr)); // 6
// Array name arr Represents the address of the first element of the array , Add 0 The address of the first element of the array ,strlen encounter '\0' end , The resulting length does not include '\0'
printf("%d\n", strlen(arr + 0)); // 6
// Array name arr Represents the address of the first element of the array ,*arr Get the first element a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
printf("%d\n", strlen(*arr)); // error
// ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
printf("%d\n", strlen(arr[1])); // error
// &arr Get the address of the entire array ,strlen encounter '\0' end , The resulting length does not include '\0'
printf("%d\n", strlen(&arr)); // 6
// &arr Get the address of the entire array , Add 1 Get the address across the entire array , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&arr + 1)); // random number , Test for 12
// Address of the first element of the array , Add 1 Get the address of the next element ,strlen encounter '\0' end , The resulting length does not include '\0'
printf("%d\n", strlen(&arr[0] + 1)); // 5
}
7.4 String pointer
void test4()
{
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));
}
Reference resources :
void test4()
{
char* p = "abcdef";
// p Point to the first address of the string , Address size 4 byte
printf("%d\n", sizeof(p)); // 4
// p Point to the first address of the string , Add 1 Point to the next element address , Address size 4 byte
printf("%d\n", sizeof(p + 1)); // 4
// p Point to the first address of the string ,*p Get the first element of the string , The type is char size 1 byte .*p=*(p+0)=p[0]
printf("%d\n", sizeof(*p)); // 1
// The first element of a string , The type is char size 1 byte
printf("%d\n", sizeof(p[0])); // 1
// &p Get the pointer variable p Address in the stack , Address size 4 byte
printf("%d\n", sizeof(&p)); // 4
// &p Get the pointer variable p Address in the stack , Add 1 Get in stack p Next address for , The type is char** size 4 byte
printf("%d\n", sizeof(&p + 1)); // 4
// The address of the first element of the string , Add 1 Get the address of the next element , The type is char* size 4 byte
printf("%d\n", sizeof(&p[0] + 1)); // 4
// p Point to the first address of the string ,strlen encounter '\0' end , The resulting length does not include '\0'
printf("%d\n", strlen(p)); // 6
// p Point to the first address of the string , Add 1 Get the address of the next element , Point to b
printf("%d\n", strlen(p + 1)); // 5
// p Point to the first address of the string ,*p Get the first element of the string a,a As strlen Parameters of , The calculated address is a Of ascii Code value 97=0x61 String length at , A memory access exception error will appear ,error
printf("%d\n", strlen(*p)); // error
// ditto , The calculated address is b Of ascii Code value 98=0x62 String length at , A memory access exception occurred ,error
printf("%d\n", strlen(p[0])); // error
// &p Get the pointer variable p Address in the stack , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&p)); // random number , Test for 3
// &p Get the pointer variable p Address in the stack , Add 1 Get in stack p Next address for , because '\0' The location is unknown , Here is a random number
printf("%d\n", strlen(&p + 1)); // random number , Test for 11
// The address of the first element of the string , Add 1 Get the address of the next element , namely b The address of
printf("%d\n", strlen(&p[0] + 1)); // 5
}
7.5 Two dimensional array
void test5()
{
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]));
}
Reference resources :
void test5()
{
int a[3][4] = {
0 };
// sizeof( Array name ) Calculate the size of the entire array
printf("%d\n", sizeof(a)); // 48
// Two dimensional array a The first element of is a[0], One dimensional array a[0] First element of a[0][0] type int size 4 byte
printf("%d\n", sizeof(a[0][0])); // 4
// a[0] Is the first element of a two-dimensional array , It is also a one-dimensional array , The type is int[4],sizeof( Array name ) Calculate the size of the entire array
printf("%d\n", sizeof(a[0])); // 16
// a[0] It's a one-dimensional array , The array name indicates the address of the first element of the array , Add 1 Represents the address of the next element , Equivalent to &a[0][0]+1, namely a[0][1] The address of
printf("%d\n", sizeof(a[0] + 1)); // 4
// a[0] It's a one-dimensional array , The name of the array is the address of the first element of the array , Add 1 Get the address of the next element ,* Get the value corresponding to the address , namely a[0][1] Place the value of the , The type is int size 4 byte
printf("%d\n", sizeof(*(a[0] + 1))); // 4
// 2D array name a Represents the first element of an array a[0] The address of , Add 1 Represents an array a The address of the next element , namely a[1] The address of
printf("%d\n", sizeof(a + 1)); // 4
// a+1 obtain a[1] The address of ,* Get a one-dimensional array a[1], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array . amount to sieof(a[1])
printf("%d\n", sizeof(*(a + 1))); // 16
// a[0] It's a one-dimensional array ,&a[0] Get the address of the whole array , Add 1 Get over a one-dimensional array a[0] The address of , namely a[1] The address of
printf("%d\n", sizeof(&a[0] + 1)); // 4
// &a[0]+1 obtain a[1] The address of ,* Get a one-dimensional array a[1], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array .
printf("%d\n", sizeof(*(&a[0] + 1))); // 16
// 2D array name a Represents the first element of an array a[0] The address of ,* Get the first element of a one-dimensional array a[0], Among them is 4 individual int Type element ,sizeof( Array name ) Calculate the size of the entire array .*a=*(a+0)=a[0] sizeof(*a)=sizeof(a[0])
printf("%d\n", sizeof(*a)); // 16
// a[3] The type of int[4]
printf("%d\n", sizeof(a[3])); // 16
}
7.6 Array pointer
Array pointers can receive two-dimensional arrays . Understand the above two-dimensional array , The following questions are not very difficult , So I won't write an analysis , Those who are interested can analyze by themselves .
Main application :*(arr + n) = arr[n]
void test6()
{
int nAry[2][4] = {
{
10,20,30,40},
{
60,70,80,90}
};
int(*p)[4] = nAry;
printf("%p\r\n", p);
printf("%p\r\n", *p);
printf("%p\r\n", (void*)**p);
printf("%p\r\n", (void*)sizeof(p));
printf("%p\r\n", (void*)sizeof(*p));
printf("%p\r\n", (void*)sizeof(**p));
printf("%p\r\n", p + 1);
printf("%p\r\n", *p + 1);
printf("%p\r\n", (void*)(**p + 1));
printf("%p\r\n", p[1] + 1);
printf("%p\r\n", (void*)*((p + 1)[1]));
printf("%p\r\n", (void*)(*(p + 1))[1]);
// Subscript operation has priority over * Operation has high priority
printf("%p\r\n", (void*)(*p[1] + 1));
printf("%p\r\n", (void*)(p[1] + 1)[1]);
}
边栏推荐
- [new open source project] dynamic configuration task scheduling framework gobrs async joins the dromara open source community
- Location data fusion Table 3
- B_QuRT_User_Guide(18)
- 深入解析问号表达式
- Solr initialization failure: requesthandler INIT failure
- C语言指针
- 二叉树最小最低公共祖先
- Cypress 88359 WL command enable hotspot
- JS memory leak
- pip 安装 qt5 。
猜你喜欢

VMware virtual machine IP, gateway settings. The virtual machine cannot be pinged to the Internet

CPT 102_ LEC 16

Unity项目优化详解(持续补充ing)

Go language advantages and learning Roadmap

巴歇尔槽流量计远程采集物联网关在明渠流量监测的应用

CPT 102_ LEC 15

GraphAcademy 課程講解:《Neo4j 圖數據科學基礎》

Helm deploy traifik ingress

Log4j:error category option "1" not a decimal integer

ASLR
随机推荐
msg=SolrCore ‘collection1‘ is not available due to init failure: Could not l
Pyqt5:slider slider control
OpenJudge NOI 1.13 18:Tomorrow never knows?
ROS基础 - 使用 launch 文件(一) - 批量启动多个ROS节点
GraphAcademy 课程讲解:《Neo4j 图数据科学基础》
VMware virtual machine IP, gateway settings. The virtual machine cannot be pinged to the Internet
Android WiFi hide SSID
新来的同事问我 where 1=1 是什么意思???
B / Qurt Utilisateur Guide (19)
postgresql源码学习(21)—— 故障恢复②-事务日志初始化
Forest v1.5.22 release! Kotlin support
位置数据融合表3
Problèmes de classe d'outils JDBC
WinDbg-虚拟机-双机调试-驱动文件的调试
OpenJudge NOI 1.13 17:文字排版
Deep parsing of question mark expressions
配置用命令行编译的环境-MSVC
OpenJudge NOI 1.13 18:Tomorrow never knows?
postgresql源码学习(二十)—— 故障恢复①-事务日志格式
B_QuRT_User_Guide(17)