Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a modern term for "array/pointer equivalence"?

Just about everyone reading this is probably familiar with these three key facts about C:

  1. When you mention the name of an array in an expression, it evaluates (most of the time) to a pointer to the array's first element.
  2. The "array subscripting" operator [] works just as well for pointers as it does for arrays.
  3. A function parameter that seems to be an array actually declares a pointer.

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

like image 561
Steve Summit Avatar asked Feb 19 '18 14:02

Steve Summit


People also ask

Is the pointer to a block of memory equivalent to an array?

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.

What do we mean by the equivalence between pointers and arrays in C?

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

Is pointer similar to array?

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*.

Why array name can also be called as a pointer?

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.


1 Answers

  1. 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.

  1. 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.)

like image 135
Lundin Avatar answered Sep 23 '22 05:09

Lundin