table of Contents:
1. What is a pointer?
(1) The pointer is a variable, the variable used to store the address (the pointer is the address)
(2) The size of the pointer is a fixed 4/8 bytes (32-bit platform/64-bit platform) a is an ordinary integer variable, p is a pointer variable used to store the address of variable a
eg: int a = 10; //在内存中开辟一块空间
int * p = &a;//对变量a取出它的地址
//将a的地址存放在p变量中,p就是一个指针变量
2. Pointer dereference
"Dereference": The function of "*" is to refer to the value of the variable pointed to by the pointer, and the function of quoting is to refer to the address of the variable
eg: int a = 10;
int* p = &a;
*p = 5;//这就是一个解引用
3. Pointer array and array pointer
Pointer array: is an array, +1 plus the size of a type
eg:int* arr[10]
Array pointer: is a pointer, +1 plus the size of an array
eg:int(*arr)[10]
4. Array name and & array name
int arr[20];
arr is the array name, the array name represents the address of the first element of the array.
Special case: sizeof(arr) represents the size of the entire array
& arr is the address of the array (array pointer)
5. Example question for pointer and array size
Example 1. Find the output of the following code
#include<stdio.h>
int main()
{
int a[] = {
1, 2, 3, 4 };
printf("%d\n", sizeof(a));//16
printf("%d\n", sizeof(a + 0));//4首元素地址加减之后代表数组里元素的地址
printf("%d\n", sizeof(*a));//4首元素的地址解引用代表首元素
printf("%d\n", sizeof(a + 1));//4
printf("%d\n", sizeof(a[1]));//4
printf("%d\n", sizeof(&a));//4数组指针
printf("%d\n", sizeof(*&a));//16数组指针解引用是一个数组
printf("%d\n", sizeof(&a + 1));//4
printf("%d\n", sizeof(&a[0]));//4
printf("%d\n", sizeof(&a[0] + 1));//4
return 0;
}
Example 2. Find the output of the following code
#include<stdio.h>
int main()
{
char arr[] = {
'a', 'b', 'c', 'd', 'e', 'f' };
printf("%d\n", sizeof(arr));//6
printf("%d\n", sizeof(arr + 0));//4,首元素地址是一个char*
printf("%d\n", sizeof(*arr));//1,首元素地址char*解引用char
printf("%d\n", sizeof(arr[1]));//1
printf("%d\n", sizeof(&arr));//4
printf("%d\n", sizeof(&arr + 1));//4
printf("%d\n", sizeof(&arr[0] + 1));//4首元素地址char*加1还是char*
return 0;
}
Example 3. Find the output of the following code
#include<stdio.h>
int main()
{
char p[] = "abcdef";
printf("%d\n", sizeof(p));//7
printf("%d\n", sizeof(p + 1));//4
printf("%d\n", sizeof(*p));//1
printf("%d\n", sizeof(p[0]));//1
printf("%d\n", sizeof(&p));//4
printf("%d\n", sizeof(&p + 1));//4
printf("%d\n", sizeof(&p[0] + 1));//4
return 0;
}
Example 4. Find the output of the following code
#include<stdio.h>
int main()
{
char *p = "abcdef";
printf("%d\n", sizeof(p));//4,是一个指针
printf("%d\n", sizeof(p + 1));//4,char*
printf("%d\n", sizeof(*p));//1,char
printf("%d\n", sizeof(p[0]));//1
printf("%d\n", sizeof(&p));//4,//char*取地址char**
printf("%d\n", sizeof(&p + 1));//4
printf("%d\n", sizeof(&p[0] + 1));//4,char取地址char*
return 0;
}
6. Examples of open space for pointers and arrays
#include <stdio.h>
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
char *str3 = "hello bit.";
char *str4 = "hello bit.";
if(str1 ==str2)
printf("str1 and str2 are same\n");
else
printf("str1 and str2 are not same\n");
if(str3 ==str4)
printf("str3 and str4 are same\n");
else
printf("str3 and str4 are not same\n");
return 0;
}