# C语言二维数组和二重指针详解

## 二维数组

type arrayName [x][y];

/** 定义数组 */
int main()
{
int		ar[3][4];		// 3 行 4列 未初始化
char	br[3][4];
double	cr[3][4];
return 0;
}


int x[3][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}};

int main()
{
int ar[][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };		// 3 行 4 列
int br[][4] = { {1,2},{3,4},{5,6} };				// 3 行 4 列 数字不足自动补 0
int cr[][4] = { 1,2,3,4,5,6,7,8 };					// 2 行 4 列
return 0;
}



#include <stdio.h>

int main()
{
int a[][3] = {1, 2, 3, 4, 5};
/* 我的第一个 C 程序 */
printf("Hello, World! %d\n", a[0][2]);//Hello, World! 3
printf("Hello, World! %d\n", a[1][2]);//Hello, World! 0

return 0;
}


#include<stdio.h>
int main(void){
int a[3]={1,2,3};
int *p;
p=a;
printf(" a=%d\n",a);
printf(" &a[0]=%d\n",&a[0]);
printf(" &a=%d\n",&a);
printf(" *a=%d\n",*a);
printf(" *p=%d\n",*p);
}


#include <stdio.h>

int main()
{
int a[3][4] = {{1, 2 , 3, 4}, {5, 6 , 7, 8}, {9, 10 , 11, 12}};

printf("Hello, World! %x\n", a);
printf("Hello, World! %x\n", a[0]);
printf("Hello, World! %x\n", a[1]);
printf("Hello, World! %x\n", a[2]);

return 0;
}


#include <stdio.h>

int main()
{
int a[3][4] = {{1, 2 , 3, 4}, {5, 6 , 7, 8}, {9, 10 , 11, 12}};

printf("Hello, World! %d\n", *(int *)(a));
printf("Hello, World! %d\n", *((int *)a[0] + 1));
printf("Hello, World! %d\n", *(int *)a[1]);
printf("Hello, World! %d\n", *((int *)a[2] + 3));

return 0;
}


#include <stdio.h>

int main()
{
int a[][3] = {{1, 2, 3},{4, 5, 6}};

printf("result is %d\n", **a);
printf("result is %d\n", *a[0]);
printf("result is %d\n", *a[1]);

return 0;
}


char a[][10] = {"be", "happy", "everyday"};

#include <stdio.h>

int main()
{
char a[][10] = {"be", "happy", "everyday"};

printf("result is %s\n", *a);
printf("result is %s\n", a[0]);
printf("result is %s\n", a[1]);
printf("result is %s\n", a[2]);

printf("result is %c\n", **a);
printf("result is %c\n", *a[0]);
printf("result is %c\n", *a[1]);
printf("result is %c\n", *a[2]);

return 0;
}


#include <stdio.h>

int main()
{
char *p = "everyday";

printf("result is %s\n", p);

return 0;
}


从哪里开始打印，就打印其之后的部分字符串

#include <stdio.h>

int main()
{
char *p = "everyday";

printf("result is %s\n", p + 5);

return 0;
}


char a[][10] = {"be", "happy", "everyday"};

char *a[] = {"be", "happy", "everyday"};

#include <stdio.h>

int main()
{
char *a[] = {"be", "happy", "everyday"};

printf("result is %s\n", *a);
printf("result is %s\n", a[0]);
printf("result is %s\n", a[1]);
printf("result is %s\n", a[2]);

printf("result is %c\n", **a);
printf("result is %c\n", *a[0]);
printf("result is %c\n", *a[1]);
printf("result is %c\n", *a[2]);

return 0;
}


## 二重指针

int **var;

void find_max_and_min(int **pmax,int **pmin, int arr[]) {
*pmax = *pmin = arr;

int i;

for(i=0;i<10;i++) {
if(**pmax < arr[i]) {
*pmax = arr+i;
}
if(**pmin > arr[i]) {
*pmin = arr+i;
}
}

}

## 补充

char s[]中s是个指针，指向首元素，首元素是个普通类型；

char *s[]中s是个二重指针，指向首元素，首元素是个字符串；

char s[][]中s是个二重指针，指向首元素，首元素是个一维数组，s指向一维数组的地址，*可得到一维数组的数组名效果；

char *s[][]中s是个三重指针，指向首元素，首元素是字符串数组，此时，*s能得到字符串数组的地址，**s能得到字符串的地址，***s能得到首字符。

• 1
点赞
• 18
收藏
觉得还不错? 一键收藏
• 1
评论
04-16 1295
01-28 1198
06-23 2万+
02-25 80
09-03
08-26
01-20
10-16 705
05-25 1303
03-04 1469
04-09 5898

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。