简体   繁体   English

将数组作为参数传递时会发生什么情况?

[英]What happens to arrays when they are passed as arguments?

I recently painfully learned that if you pass in array to a function, it 'decays' to a pointer. 最近,我痛苦地了解到,如果将数组传递给函数,则它会“衰减”到指针。

My question is then, what is the array treated as in the scope in which it was created and how does sizeof tell the difference? 那么我的问题是,在创建数组的范围内将其视为什么?sizeof如何区分两者之间的区别? I thought all arrays were pointers and the square brackets moved the pointer forward and dereferenced in one go. 我以为所有数组都是指针,并且方括号将指针向前移动并一次性取消引用。 Observe the following code: 观察以下代码:

[name@localhost lab03]$ cat arraysAsArguments.c
#include <stdio.h>

void function(int array[]) //Treated as pointer
{
  printf("%x", array);
  int size = sizeof array;
  int firstElement = sizeof array[0];
  printf("size: %d\n", size);
  printf("firstElement: %d\n", firstElement);
}

int main()
{
  int array[] = {0,1,2,3,4,5}; // expected size = 6*4 = 24 bytes
  printf("%x", array);
  int size = sizeof array;
  int firstElement = sizeof array[0];
  printf("size: %d\n", size);
  printf("firstElement: %d\n", firstElement);
  function(array);
}

[name@localhost lab03]$ clang arraysAsArguments.c
arraysAsArguments.c:5:12: warning: conversion specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
  printf("%x", array);
          ~^   ~~~~~
arraysAsArguments.c:15:12: warning: conversion specifies type 'unsigned int' but the argument has type 'int *' [-Wformat]
  printf("%x", array);
          ~^   ~~~~~
2 warnings generated.
[name@localhost lab03]$ ./a.out
57c84940size: 24
firstElement: 4
57c84940size: 8
firstElement: 4

In both main and function, array is of type int*. 在main和function中,array均为int *类型。 What is going on?! 到底是怎么回事?!

Arrays decay to a pointer to their first element when passed as a function. 作为函数传递时,数组会衰减到指向其第一个元素的指针。 This is why your sizeof call within your function is not what you expect, because it is calling sizeof on a pointer. 这就是为什么function内的sizeof调用不符合预期的原因,因为它正在指针上调用sizeof Inside function this line 内部function这条线

int size = sizeof array;

array has already decayed to a pointer and you are storing the size of a int* in your size variable. array已经衰减到指针,并且您将int*的大小存储在您的size变量中。

You are still able to access elements within the array via the argument though as the notation 您仍然可以通过参数访问数组中的元素,尽管作为表示法

array[1];

is the same as 是相同的

*(array + 1);

This is the same way you would access array elements if you had declared your function to accept an int* directly as parameter instead of an array. 如果您声明函数直接接受int*作为参数而不是数组,则访问数组元素的方式与访问数组元素相同。 All that has happened is decay to a pointer - your int array[] in main is still an array. 所有这一切已经发生衰变是一个指针-你的int array[]中的main仍是一个数组。

Cc-faq: 6.4 : 抄送常见问题解答:6.4

Since arrays decay immediately into pointers, an array is never actually passed to a function. 由于数组会立即衰减为指针,因此数组实际上不会传递给函数。 You can pretend that a function receives an array as a parameter, and illustrate it by declaring the corresponding parameter as an array: 您可以假装一个函数接收一个数组作为参数,并通过将相应的参数声明为数组来说明它:

 void f(char a[]) { ... } 

Interpreted literally, this declaration would have no use, so the compiler turns around and pretends that you'd written a pointer declaration, since that's what the function will in fact receive: 从字面上解释,此声明将毫无用处,因此编译器转过身来并假装您编写了指针声明,因为该函数实际上将接收以下内容:

 void f(char *a) { ... } 

c-faq: Why doesn't sizeof properly report the size of an array when the array is a parameter to a function? c-faq:当数组是函数的参数时,为什么sizeof不能正确报告数组的大小? :

The compiler pretends that the array parameter was declared as a pointer (that is, in the example, as char *a; ), and sizeof reports the size of the pointer. 编译器假装将数组参数声明为指针(在本示例中为char *a; ),并且sizeof报告指针的大小。

I would suggest you to read entire section of c-faq: 6. Arrays and Pointers . 我建议您阅读c-faq的整个部分:6.数组和指针

As per your statement while passing array to function Arrays decays to pointer is correct. 按照您的声明,同时将数组传递给函数Arrays衰减到指针是正确的。

We can say arrays are nothing but pointers only, the only difference is "array are constant pointer's" . 我们可以说数组只不过是指针,唯一的区别是“数组是常量指针的”

int arr[3] = {1,2,3};

so, here we can get the array elements by using pointer dereference as %d,*(arr+0); 因此,这里我们可以通过将指针取消引用用作%d,*(arr + 0);来获取数组元素。

printf("%d %d \n",*(arr+0),arr[0]);

Also, char str = "string"; 另外,char str =“ string”; printf("%c\\n", (str+0)); printf(“%c \\ n”, (str + 0));

So, according to my understanding arrays and pointers are same, only the difference is ARRAYS ARE CONSTANT POINTERS . 因此,根据我的理解,数组和指针是相同的,唯一的区别是数组是常数POINTERS ie we can't do arr++; 即我们不能做arr ++; to array. 排列。

int arr[3];
char *str;

If we are passing array to function, whatever changes we make in arr will reflect in calling function array as arr, &arr and &arr[0] are same. 如果我们将数组传递给函数,那么我们对arr所做的任何更改都会反映在调用函数数组中,因为arr,&arr和&arr [0]相同。 Where in pointer : str and &str are different. 指针中的哪里:str和&str不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 传递给execve的参数会怎样? - What happens to the arguments passed to execve? 如果传递给sscanf的参数被强制转换会发生什么 - What happens if arguments passed to sscanf are cast 将双指针传递给函数时会发生什么 - what happens to a double pointer when it is passed to a function 没有 arguments 的 if == 语句会发生什么情况? - what happens with if == statement with no arguments? 当 arrays 通过函数作为 arguments 传递时,它们是否被零索引? C编程 - Are arrays zero indexed when they are passed throu functions as arguments? C programming 当一个指针作为指向另一个函数内部的指针的指针传递时会发生什么? - what happens when a pointer is passed as a pointer to a pointer inside another function? 内联函数作为C中的参数传递时会发生什么? - What happens when an inline function is passed as a parameter in C? 当函数调用返回时,压入堆栈的参数会发生什么变化? - What happens to arguments pushed on the stack when a function call returns? 数组是通过引用传递的,但是,如果我仅传递未存储在内存中的数组的值,会发生什么? - Arrays are passed by reference, but what happens if I only pass the value of an array which is not stored in memory? 在 C 中,作为 arguments 传递时,`&amp;function` 和 `function` 有什么区别? - In C, what is the difference between `&function` and `function` when passed as arguments?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM