简体   繁体   中英

Difference between char[] and char* in function call

I have found myself unable to explain why the following piece of code works. Needless to say, I am quite new to C++...

#include <cstdio>

void foo(char* p)
{
    p[0] = 'y';
}

int main()
{
    char a[1];
    a[0] = 'x';
    printf("a[0] = %c\n", a[0]);
    foo(a);
    printf("a[0] = %c\n", a[0]);
    return 0;
}

This program outputs

a[0] = x
a[0] = y

What intrigues me us that I am not passing a pointer, but an array, to foo. So how can foo change the value of the array a? Does this apply only to arrays of char?

The answer to Difference between char and char[1] , confirms my observation, but it does not go into detail about why this is the case.

Thanks!

When you're passing an array to a function it decays to a pointer to the first element.

The following are completely equivalent:

void foo(char* p);
void foo(char p[]);
void foo(char p[42]); /* Or any other number. */ 

Does this apply only to arrays of char?

It applies to any array. I recommend the aryptr section of the C FAQ .

In C, arrays, in most contexts (*), decay into a pointer to their first element.

In your case, the array a decays into a pointer to a[0] .

The array int arr[12][23] , when used just by its identifier, decays to a pointer to its first element, namely arr[0] , which is of type int (*)[23] (pointer to array of 23 ints).

(*) Arrays do not decay when used as the argument to the sizeof operator, or when used as argument to the & operator or when used as initializer (a string literal) for a character array.

When You say

char a[5];

the compiler allocates 5 sequential blocks of memory, and the address of the first block is assigned to a. So by definition name of the array (a in our case) is just a pointer to a memory location.

Now when we say a[0], compiler manipulates it as *(a+0*sizeof(array datatype ie char, int etc.)), symbolically a[i] is represented as *(a+i*sizeof(array datatype ie char, int etc.))

As far as function parameter passing is concerned When we pass an array to a function basically it is just the address of the first element which is passed. For more details regarding this plz follow this link or read the @cnicutar's answer (as he has already posted a correct answer there is no point repeating that again)...

You can try this to convince yourself how this works.

int a[8], *p;
p = a;
printf("%p, %p, %p\n", a, &a[0], p);

数组无非是一个指针(指向第一个元素),至少是为了传递参数。

The three prototypes here are equivalent:

void foo(char *p);
void foo(char p[]);
void foo(char p[42]);

C says a declaration of parameter of type array of T is adjusted to a declaration of type pointer to T .

Array name points to the address of the first element of the array. Please refer: Is an array name a pointer?

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