C基础--二重指针和数组指针
#include <stdio.h>
int main1(void)
{
int a = 3;
int b = 8;
int *p = &a; // int* p = &a;
int **q; // int** q;
q = &p;
**q = 10;
*q = &b;
**q = 5;
p = &b;
*p = 9;
printf("a = %d\tb = %d\n", a, b);
return 0;
}
//void fun(char *arr[], int n)
void fun(char **arr, int n) //二重指针传参时等价于指针数组
{
int i;
for (i = 0; i < n; i++)
printf("%s\n", arr[i]);
//printf("%s\n", *(arr+i)); //当成二重指针去索引元素
}
int main(void)
{
char *s[3] = {"hello", "world", "xwp"};
fun(s, 3);
return 0;
}
#include <stdio.h> int main1(void) { int a[10] = {1,2,3,4,5,6,7,8,9,0}; //int *p; int (*p)[10]; //先算小括号,p和*结合,属于指针类型,指针指向拥有10个int型元素的数组 //p = a; int *p; p = a; a + 1 p = &a; //int (*p)[10] = &a; &a + 1 p+1 加过40个字节 printf("%d\t%d\n", sizeof(p), sizeof(*p)); printf("%d\n", (*p)[3]); return 0; } int main2(void) { /* char str[3][20] = {"hello", "world", "itcast"}; char (*p)[20]; p = &str[0]; printf("%s\n", *(p+1)); */ /* char str[3][20] = {"hello", "world", "itcast"}; char *p; p = str[0]; //printf("%s\n", p); printf("%s\n", (p+20)); */ char str[3][20] = {"hello", "world", "itcast"}; char (*p)[20]; p = str; printf("%s\n", *(p+1)); return 0; } //void fun(char a[][20], int n) //数组指针传参时等价于二维数组 void fun(char (*a)[20], int n) { int i; for (i = 0; i < n; i++) scanf("%s", a[i]); return; } int main(void) { int i; char s[3][20]; fun(s, 3); for (i = 0; i < 3; i++) printf("%s\n", s[i]); return 0; }