🥑🍓Let's go🍓🥑
一:字符指针
🍃🍃例如:
1:
#include <stdio.h>
int main()
{
char ch = 'w';
char* pc = &ch;
*pc = 'a';
printf("%c", ch);
return 0;
}
2:
#include <stdio.h>
int main()
{
char ch = "abcd";
char *pc = &ch;
*pc = 'f';
printf("%s", ch);
return 0;
}
//此时会报错,“abcd”是常量字符串,不可以进行修改。
存入指针的是字符串的首字符的地址
3:
#include <stdio.h>
int main()
{
const char* pstr = "hello bit.";
printf("%s\n", pstr);
return 0;
}
#include <stdio.h>
int main()
{
char str1[] = "hello bit.";
char str2[] = "hello bit.";
const char* str3 = "hello bit";
const 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;
}
//输出的结果是
str1 and str2 are not same
str3 and str4 are same
//原因如下:str1与str2所指的是两个不同的空间,而*str3与*str4所指向的是同一个字符串
二:指针数组(存放指针的数组)
🍃🍃例如:
#include <stdio.h>
int main()
{
int* arr[10];
char* arr[10];
char** arr[10];
}
三:数组指针(指向整型数据的指针)
3.1 数组指针的定义
//指针数组
int* p1[10];
//数组指针
int(*p)[10];
【注】:
解释:
p
先和
*
结合,说明
p
是一个指针变量,然后指着指向的是一个大小为
10
个整型的数组。所以
p是一个指针,指向一个数组,叫数组指针。
//
这里要注意:
[]
的优先级要高于
*
号的,所以必须加上()来保证
p
先和
*
结合。
3.2 &数组名VS数组名
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int* pa = arr;//此时的pa的值是arr[0]的地址
int* pb = &arr;//此时的pb的值是整个数组的地址
printf("%p\n", arr);
printf("%p\n", &arr);
return 0;
}
【注】:数组名大部分就是数组首元素地址
1.sizeof(数组名):sizeiof内部单独放置一个数组名的时候,数组名表示整个数组,计算并得到数组的总大小;
2.&arr,这里表示数组名是整个数组,取出的是整个数组的地址,从地址的角度来说和数组的首元素的地址是一样的,但是意义不一样
🍂🍂例如:
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
printf("arr=%p\n", arr);
printf("&arr=%p\n", &arr);
printf("arr+1=%p\n", arr+1);
printf("&arr+1=%p\n", &arr + 1);
return 0;
}
结果得到:
【注意】此时表明&数组名是整个数组的地址,&数组名+1表示跳过一整个数组的地址
3.3数组指针的使用
3.3-1 数组指针的定义
🍁🍁例如:
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int(*pa)[10] = &arr;//把数组arr的地址赋值给数组指针变量pa
return 0;
}
3.3-2 数组指针的使用
❄️❄️例如:
void print1(int arr[3][5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
void print2(int(*arr)[5], int row, int col)
{
int i = 0;
for (i = 0; i < row; i++)
{
int j = 0;
for (j = 0; j < col; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
}
#include <stdio.h>
int main()
{
int arr[3][5] = { 1,2,3,4,5,6,7,8,9,10 };
print1(arr, 3, 5);
print2(arr, 3, 5);
return 0;
}
四:数组参数、指针参数
4.1一维数组传参
void test(int arr[])
{
}
void test(int arr[10])
{
}
void test(int* pa)
{
}
void test2(int* arr2[10])
{
}
void test2(int** arr)
{
}
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int* arr2[10] = { 0 };
test1(arr);
test2(arr2);
return 0;
}
【注意】:
一维数组传参,形参可以是数组也可以是指针。当形参是指针的时候,要注意类型。
4.2 二维数组传参
void test(int arr[3][5])
{
}
void test(int arr[][5])
{
}
void test(int(*arr)[5])
{
}
#include <stdio.h>
int main()
{
int arr[3][5] = { 0 };
test(arr);
return 0;
}
【注意】:
二维数组传参,参数可以是指针也可以是数组。如果是数组,行可以省略但是列不可以省略。
如果是指针,传过去的是第一行的地址,形参就应该是数组指针。
二维数组的数组名是第一行数组的地址。
4.3 一级指针传参
🍆🍆例如:
void print(int* p, int sz)
{
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
}
#include <stdio.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
int* pa = arr;
int sz = sizeof(arr) / sizeof(arr[0]);
print(arr, sz);
return 0;
}
【注意】:
传的是一级指针,则形参也是一级指针接收。
4.4 二级指针传参
🍆🍆例如:
#include <stdio.h>
test1(int** ptr)
{
}
test2(int** ptr)
{
}
int main()
{
int n = 10;
int* p = &n;
int** pp = &p;
test1(&p);
test2(pp);
return 0;
}
void test(char** p)
{
}
#include <stdio.h>
int main()
{
char c = 'b';
char* pc = &c;
char** ppc = &pc;
char* arr[10];
test(&pc);
test(ppc);
test(arr);
return 0;
}
【注意】:
二级指针传参,形参就用二级指针接收