user1083059
user1083059

Reputation: 160

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!

Upvotes: 1

Views: 3676

Answers (7)

omggs
omggs

Reputation: 1153

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

Upvotes: 0

foo
foo

Reputation: 387

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

Upvotes: 1

pmg
pmg

Reputation: 108967

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.

Upvotes: 5

Amit
Amit

Reputation: 13364

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 i.e. char, int etc.)), symbolically a[i] is represented as *(a+i*sizeof(array datatype i.e. 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)...

Upvotes: 1

ouah
ouah

Reputation: 145829

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.

Upvotes: 0

Michel Keijzers
Michel Keijzers

Reputation: 15347

An array is nothing more than a pointer (to the first element), at least for argument passing sake.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182609

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.

Upvotes: 5

Related Questions