Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when does a array act as a pointer in c?

i know that the array name could be used as a pointer(although it's a converted form), but my question is , Is there some other instance where array acts as a pointer.

like image 371
vetional Avatar asked Aug 10 '26 14:08

vetional


2 Answers

Technically speaking, an array name never acts as a pointer. An expression with array type (which could be an array name) will convert to a pointer anytime an array type is not legal, but a pointer type is. And the declaration of an array as a function parameter is converted into a declaration of a pointer. (Which means that the name isn't an array name, but a pointer name. Despite appearances.)

like image 142
James Kanze Avatar answered Aug 13 '26 03:08

James Kanze


It is due to array-to-pointer standard conversion. Specifically, array decays into a pointer to the first element of the array.

Array decays into a pointer, I think, because arrays and pointers are used exactly in the same way: using an index, which you can increment and decrement, to traverse the elements, in both direction, forward and backward..

like image 41
Nawaz Avatar answered Aug 13 '26 03:08

Nawaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!