1

New to C. When I declare the following array:

char arr [3] = {'a', 'b', 'c'};

What does just the following represent?

 arr

What's the difference between the following? What does each one represent specifically?

arr and &arr

What happens when you pass in the starting point of an array as a parameter to a function that accepts a 1D array? Are the values copied over to a new chunk of memory?

I understand that in C, an array is simply an allocated chunk of blocks of memory that are grouped together? Is that right? I'm trying to wrap my head around arrays in C, but I'm kind of confused. Please provide as much insight as you can.

Vimzy
  • 1,871
  • 8
  • 30
  • 56
  • `arr` is of type `char *`, `&arr` is of type `char[3] *`, their value is the same – mangusta Feb 12 '15 at 05:20
  • read this link [http://stackoverflow.com/questions/8916656/why-is-arr-and-arr-the-same] – Arun A S Feb 12 '15 at 05:27
  • @mangusta: `&arr` is of type `char (*)[3]`, a pointer to an array of 3 `char`. You have the `*` in the wrong place and with insufficient parentheses. – Jonathan Leffler Feb 12 '15 at 05:40
  • @JonathanLeffler i know that, still i'm used to write a pointer to array that way : ) it looks simpler for me – mangusta Feb 12 '15 at 05:43
  • @mangusta: Hmmm; it looks non-compilable to me — and to the average C compiler — which isn't very helpful to someone who needs to ask the question. – Jonathan Leffler Feb 12 '15 at 05:44
  • @JonathanLeffler yeah, it's non-compilable )) just my personal format. you're right, i was supposed to write it in a proper way when replying to author – mangusta Feb 12 '15 at 05:45

2 Answers2

3

arr &arr and &arr[0]

char arr[3] = {'1','2','3'};

--------------------
|  1    | 2   |  3 |
--------------------
   |
   |
arr &arr and &arr[0]

So there is no difference when it comes to what these contain. All have the starting address of the array.

In C array is a contiguous collection of elements of similar data-type.

As mentioned what they contain is same but make a note of the below points.

&arr gives the address let's say 0x1000. arr also gives 0x1000.

Now incrementing the pointer arr will give you the address of the next element of the array which is &arr[1]

`arr+1` != `&arr+1`

With &arr+1 the value &arr is getting incremented by the size of the array (in this case, 3); it is not pointing to the next element in the array as the pointer does.

So they are both different types and should be used with this in mind.

PS: Array name is not a modifiable lvalue.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Gopi
  • 19,784
  • 4
  • 24
  • 36
  • What happens when you pass in the starting point of an array as a parameter to a function that accepts a 1D array? Are the values copied over to a new chunk of memory? – Vimzy Feb 12 '15 at 05:18
  • @Gopi, I answered no for Vimzy's question, not for your answer. That is the values are not copied over to a new chunk of memory. I was typing this answer when you answered and so didn't see your post. Sorry if it offended you. :) – Arun A S Feb 12 '15 at 05:26
1

arr &arr and &arr[0] all store the address of the first value in your array that is the address of arr[0].

They are all pointers.

for your comment

What happens when you pass in the starting point of an array as a parameter to a function that accepts a 1D array? Are the values copied over to a new chunk of memory?

the answer is that when the address is passed, whatever changes are made to it is reflected on the original array.

Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • 1
    Note that the type of `arr` and `&arr[0]` is different from the type of `&arr`. Also, the behaviour of `arr + 1` and `&arr + 1` is quite different in general. – Jonathan Leffler Feb 12 '15 at 05:42