C语言中,指针,引用,二维数组,指针数组,数组指针的解析

时间:2022-09-06 08:42:34
//二维数组的理解
#include<stdio.h>
void main()
{
int a[3][3] = {1,2,3,4,5,6,7,8,9};
int *n[3]; //这两种声明的方式是一样
int *(n[3]); //指针数组
int (*p)[3] = a; //数组指针,指向数组的指针
int *m =&a[0][0];
printf(
"%x\n",m);
printf(
"%x\n",*(p+1));
printf(
"%d\n",**(p+1));
printf(
"%x\n",*a);
printf(
"%x\n", a);
for(int i=0;i<9;i++)
printf(
"%x\n",*(a+i));
for(int i=0;i<9;i++)
printf(
"%d\n", *(*a+i));
for(int i=0;i<9;i++)
printf(
"%d\n",**(a+i));
}


//指针参数也就是一个地址而已
#include<stdio.h>
void print(int *q)
{
printf(
"%x\n",q);
q
= q+3;
printf(
"%x\n", *q);
}

void main()
{
int b[9] = { 1,2,3,4,5,6,7,8};
int *p = b;
printf(
"%x\n",p);
print(p);
printf(
"%x\n",*p++);
}

对指针引用的理解
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
void Malloc(char *&p)
{
printf(
"%x\n",p);
p
= (char*)malloc(100);
printf(
"%x\n",p);
}

void main()
{
char *str = NULL;
str
= (char*)malloc(100);
strcpy(str,
"hello");
printf(str);
strcpy(str,
"world\n");
printf(str);
printf(
"%x\n",str);
free(str);
str
= NULL;
printf(
"%x\n",str);
Malloc(str);
strcpy(str,
"hello");
printf(str);
}