Just about everyone reading this is probably familiar with these three key facts about C:
[]
works just as well for pointers as it does for arrays.These three facts are absolutely central to array and pointer handling in C. They're not even three separate facts; they're interlinked facets of one central concept. It is not possible to do even fairly basic C programming properly without a decent understanding of this concept.
My question today is simply, What is the name for this concept?
I supposed I'm old-fashioned, but I've always called it "The equivalence between arrays and pointers in C", or "array/pointer equivalence" for short. But I've learned that you almost can't say those words on SO; they're pretty much taboo.
This may seem like an abstract or philosophical question, so to frame it more concretely, what I'm looking for is is a simple noun or noun phrase I could use in the sentence "Yes, due to _____, array subscripting can be thought of as syntactic sugar for pointer arithmetic", in answer to, say, this question.
(But please note that I am not looking for answers to that question, or for answers to the question "What's wrong with the word 'equivalence'?". Yes, I know, it can mislead learners into imagining that arrays and pointers are somehow the same. I did have that confusion in mind when I wrote this FAQ list entry.)
4) A pointer to a block of memory is effectively same as an array. The correct option is (a). Explanation: Using the standard library function malloc() and treat it as an array.
Pointer arithmetic and array indexing are equivalent. In other aspects, pointers and arrays are different. Here's an example displaying the equivalence: #include <stdio.h> int main() { char arr[] = "don't panic\n"; char* ptr = arr; printf("%c %c\n", arr[4], ptr[4]); printf("%c %c\n", *(arr+2), *(ptr+2)); return 0; }
An array is considered to be the same thing as a pointer to the first item in the array. That rule has several consequences. An array of integers has type int*.
An array name contains the address of first element of the array which acts like constant pointer. It means, the address stored in array name can't be changed. For example, if we have an array named val then val and &val[0] can be used interchangeably.
- The "array subscripting" operator [] works just as well for pointers as it does for arrays.
No, in fact it only works for pointers. Whenever you type []
in an expression, you always get a pointer to the first element. This is guaranteed to happen since arr[i]
must be equivalent to *(arr + i)
. The former is "syntactic sugar" for the latter.
- A function parameter that seems to be an array actually declares a pointer.
This is actually a special case, referred to as "array adjustment", where the compiler implicitly changes the declaration of a function parameter of array type into a pointer to the first element. The rationale is surely to make functions compatible with the "array decay" of expressions, but the C standard keeps the terms separate.
Both cases, expressions and function parameters, are often referred to informally as "array decay". Though sometimes this is only used for expressions and not for function parameters. I don't think there exists a single, consistent use of the term. "Array decay" is the best one I think, although the C standard does not use that term anywhere.
(I dislike the term "equivalence", because an array can turn into a pointer, but not the other way around. Indeed there's always countless beginners coming up with confused beliefs such as "arrays and pointers are the same thing". Calling them "equivalent" doesn't exactly help.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With