C语言 数组与指针的理解

#include "stdio.h"

#include "stdlib.h"

void func( int a[] );

int main( int argc, char* argv[] )
{

 /*
 this program prove below:
  Except when it is the operand of the sizeof operator 
  or the unary & operator, or is a string literal used to initialize an array, 
  an expression that has type ‘‘array of type’’ is converted to 
  an expression with type ‘‘pointer to type’’ that points to the initial element of the array object 
  and is not an lvalue.

  -- C99

  数组在除了3种情况外, 其他时候都要"退化"成指向首元素的指针.  
  这3中例外情况是:
  (1) sizeof(s)
  (2) &s;
  (3) 用来初始化s的"china";

  -- C99

  另外,从数组退化成的指针,不可执行++操作,即可以理解为const指针。
  这里需要注意另外一点,即,函数 void func( int a[] ),在编译过程中,将参数int a[]也解释为 int *
  (遵循C99),
  这样,在函数内部,a++是允许的。
 */

 int x = 6;
 int * p = &x;
 printf("the value of point p:%d\n",*p);


 int y[4] = {1,2,3,4};

 printf("the value of array name y:%d\n", *y);
 printf("the value of array name y:%d\n", *(y+1));
 printf("the value of array name y:%d\n", *(y+2));
 printf("the value of array name y:%d\n", *(y+3));
 

 int * q = y;
 printf("the value of point q:%d\n",*q);
 printf("the value of point q+1:%d\n",*(++q));
 printf("the value of point q+2:%d\n",*(++q));
 printf("the value of point q+3:%d\n",*(++q));


 /* 2D array ->  1D pointer */
 int z[3][2] = {{1,2},{5,6},{9,10}};  // 数组在内存中的分配是连续的
 int * r = NULL;
 r = (int *)&z;
 printf("the value of point r:%d\n",*r);
 printf("the value of point r+1:%d\n",*(r+1));
 printf("the value of point r+2:%d\n",*(r+2));
 printf("the value of point r+3:%d\n",*(r+3));

 r++;
 printf("the value of point r:%d\n",*r);
 printf("the value of point r+1:%d\n",*(r+1));
 printf("the value of point r+2:%d\n",*(r+2));
 printf("the value of point r+3:%d\n",*(r+3));

 int *s = z[0];
 printf("the value of point s:%d\n",*r);
 printf("the value of point s+1:%d\n",*(r+1));
 printf("the value of point s+2:%d\n",*(r+2));
 printf("the value of point s+3:%d\n",*(r+3));


 /* 2D array  ->  pointer of array. */ //in this segment, [x][y] is changed to pointer address plus (x-1)*y + y
 int (*t)[2] = (int (*)[2])z;
 printf("t's content: %d\n", t[0][0]);
 printf("t's content: %d\n", t[0][1]);
 printf("t's content: %d\n", t[1][0]);
 printf("t's content: %d\n", t[1][1]);

 printf("t's content: %d\n", t[0][2]);
 printf("t's content: %d\n", t[0][3]);
 printf("t's content: %d\n", t[0][4]);

 int (*tt)[6] = (int (*)[6])z;
 printf("tt's content: %d\n", tt[0][0]); // 这里把tt[0]理解为指针tt
 printf("tt's content: %d\n", tt[0][1]);
 printf("tt's content: %d\n", tt[0][2]);
 printf("tt's content: %d\n", tt[0][3]);
 printf("tt's content: %d\n", tt[0][4]);


 /* 1D  -> pointer of array. */
 int a[3] = {1,2,3};
 //int *po = a;
 int (*poo)[3] = (int (*)[3])&a;

 printf("poo's content: %d\n", poo[0][0]); // 这里把poo[0]理解为指针poo
 printf("poo's content: %d\n", poo[0][1]);
 printf("poo's content: %d\n", poo[0][2]);


 int b[3] = {2,3,4};
 func(b);

 printf("end\n");
}

void func( int a[] )
{
 printf(" in func, a is: %d\n", *a);
 printf(" in func, ++a is: %d\n", *(++a));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值