将数组传递给函数使用C的指针 [英] Passing an array to a function makes a pointer in C

查看:225
本文介绍了将数组传递给函数使用C的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现的东西,我无法解释C.
当你使用数组,你它发送给一个函数,无论怎样这个功能接收到这个阵列(如数组或一个指针),它会像它是一个指针。
我不知道如果我真的所以这里明确的是一个小例子。

I just discovered something that I can't explain in C. When you're working with an array and you send it to a function, no matter how this function receives this array (as an array, or as a pointer), it will act like it's a pointer. I don't know if I am really clear so here is a little example.

#include        <stdio.h>

void            modify(char foo[])
{
  printf("foo's address = %p\n", &foo);
  printf("foo's value (pointer) : %p\n", foo);
  foo[0] = 'Z';
}

int             main()
{
  char          foo[] = "hello";

  printf("foo's address = %p\n", &foo);
  printf("foo's value (pointer) : %p\n", foo);
  modify(foo);
  printf("%s\n", foo);
}

下面是输出:

foo's address = 0x7fff9a1a47b0
foo's value (pointer) : 0x7fff9a1a47b0
foo's address = 0x7fff9a1a4798
foo's value (pointer) : 0x7fff9a1a47b0
Zello

如果我声明一个数组,因为它的地址是它的地址是第一要素,两个第一的printf来电显示同样的事情。
但是,当被修改功能收到此阵,再次为一个数组,未来两年的printf调用是不相同的。在修改功能,富是一个数组的指针。回到在主(),阵列的第一个字母已被修改。
可能有人解释说,要这样对待我?
我不经常与阵列工作,但我认为传递paramater数组功能,只会给它的一个副本(使用一个char或int时等)。

If I declare an array, as it's address is the address of it's first element, the two first "printf" calls will display the same thing. But when this array is received by the "modify" function, again as an array, the next two "printf" calls are not identical. In the modify function, "foo" is an array pointer. Going back in the main(), the first letter of the array has been modified. Could someone explain that behavior to me ? I don't often work with array, but I thought that passing an array in paramater to a function, would simply make a copy of it (like when using a char or an int).

推荐答案

根据C标准(6.3.2.1左值,数组和功能指示器)

According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3除了当它是sizeof操作符或一元和放大器的操作;
  运营商,或者是用于初始化数组文本字符串,
  具有类型'类型的阵列'前pression被转换为
  前pression型指向最初的指针键入''
  数组对象和元素不是一个左值。如果数组对象
  已注册存储类,行为是不确定的。

3 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. If the array object has register storage class, the behavior is undefined.

和(6.7.6.3函数声明符(包括原型)

And (6.7.6.3 Function declarators (including prototypes)

7的参数为''类型的数组'的声明应调整
  到'合格指向类型''...

7 A declaration of a parameter as ‘‘array of type’’ shall be adjusted to ‘‘qualified pointer to type’’...

因此​​,这些函数声明

Thus these function declarations

void            modify(char foo[]);

void            modify(char *foo);

是等价的,并宣布同一种功能。

are equivalent and declare the same one function.

功能参数是局部变量。
因此,在功能参数修改都有自己的地址。这些语句的输出

Parameters of a function are its local variables. So in function modify parameter foo has its own address. and the output of these statements

printf("foo's address = %p\n", &foo);
printf("foo's value (pointer) : %p\n", foo);

显示了这种

foo's address = 0x7fff9a1a4798
foo's value (pointer) : 0x7fff9a1a47b0

至于在主声明该阵列然后它的地址是由它占用存储器的地址。同时它是数组的第一元素的地址。而这两个语句

As for the array declared in main then its address is the address of the occupied by it memory. At the same time it is the address of the first element of the array. And these two statements

printf("foo's address = %p\n", &foo);
printf("foo's value (pointer) : %p\n", foo);

在主说明了这一点。

foo's address = 0x7fff9a1a47b0
foo's value (pointer) : 0x7fff9a1a47b0

不同的是,包含作为参数传递数组的第一个元素的地址,你输出的局部变量的地址里面的功能,而在主,你的输出数组本身就是其占用的内存地址

The difference is that inside the function you output the address of its local variable that contains the address of the first element of the array passed as an argument while in main you output the address of the array itself that is of its occupied memory.

至于在功能上这句话

foo[0] = 'Z';

那么作为富包含数组的第一个元素的地址,那么该语句将存储在该地址的值。

then as foo contains the address of the first element of the array then the statement changes the value stored at this address.

这篇关于将数组传递给函数使用C的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆