简体   繁体   中英

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? 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*. 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. Inside function this line

int size = sizeof array;

array has already decayed to a pointer and you are storing the size of a int* in your size variable.

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. All that has happened is decay to a pointer - your int array[] in main is still an array.

Cc-faq: 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? :

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.

I would suggest you to read entire section of c-faq: 6. Arrays and Pointers .

As per your statement while passing array to function Arrays decays to pointer is correct.

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);

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

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

So, according to my understanding arrays and pointers are same, only the difference is ARRAYS ARE CONSTANT POINTERS . ie we can't do 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. Where in pointer : str and &str are different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM