Chinaunix首页 | 论坛 | 博客
  • 博客访问: 336072
  • 博文数量: 82
  • 博客积分: 2602
  • 博客等级: 少校
  • 技术积分: 660
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-10 08:48
文章分类

全部博文(82)

文章存档

2008年(17)

2007年(65)

分类: C/C++

2008-05-13 14:35:45

一维数组名可以作为函数参数传递,多维数组名也可以作为函数参数传递。在用指针变量作形参以接受实参数组名传递来的地址时,有两种方法:
1、用指向变量的指针变量
2、用指向一维数组的指针变量
 

#include <stdio.h>
void fun(char (*s)[5], int n)//s是指向具有5个元素的一维数组的指针
{
        while(n--)
        {
                printf("%s\n",s);
                s++;
        }
}
int
main(void)
{
        char p[][5]={"abcd","efg","hij"};
        fun(p,3);
        return 0;
}

以下摘自ypxing博客中的《数组与指针--都是“退化”惹的祸》

http://blog.chinaunix.net/u1/35100/showart_445864.html

C99中原话:
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.上面这句话说的很清楚了, 数组在除了3种情况外, 其他时候都要"退化"成指向首元素的指针.
比如对 char s[10] = "china";
这3中例外情况是:
(1) sizeof(s)
(2) &s;
(3) 用来初始化s的"china";

除了上述3种情况外,s都会退化成&s[0], 这就是数组变量的操作方式
因此数组名作函数参数时,char a[]退化成char *a,char *b[] 退化成 char **b,char  c[n][m]退化成 char (*c)[m] 

 

 

阅读(3308) | 评论(0) | 转发(0) |
0

上一篇:posix多线程2(ZT)

下一篇:关于FIFO

给主人留下些什么吧!~~