1

A C book says "When an array identifier appears in an expression, the type of the identifier is converted from "array of T' to "pointer to T," and the value of the identifier is converted to a pointer to the first element of the array".

However the C programming language ( by Kernighan and Ritchie ) says "By definition, the value of a variable or expression of type array is the address of element zero of the array".

  1. So is it correct to say "that an array identifier is converted to a pointer that points to the first element of the array or that an array identifier is converted to the address of the first element"?

This got me a bit confused...

Consider this small code :

int a = 10;
&a;

2)Is &a a "pointer" in the general sense ( even if a pointer is technically a variable that holds the address of another variable ) ? So is an address of an object considered a "pointer" to that location in the general sense even if it is not given to a pointer variable ?

3)If I do something like ((&a) + 1) I'm technically doing pointer arithmetics, so it kind of implies that &a is a pointer in a certain way, doesn't it ?

alessio solari
  • 313
  • 1
  • 6
  • `int* b = &a;` so yes, it is a pointer. https://godbolt.org/z/4h5G44KE8 – Ted Klein Bergman Jul 27 '23 at 18:15
  • When an array is converted to a pointer, it is a pointer to the first element of the array. Just remember it this way: Elements are accessed the same way. So it you have `int arr[10];`, then if you pass `arr` to a function, it has type `int *`. So does `arr + 0`. But if you take the address, as in `&arr`, then it has type `int (*)[10]`. They are different types, and are accessed differently, even though they refer to the same address. – Tom Karzes Jul 27 '23 at 18:16
  • Tom Karzes, my doubt is if an address is considered a "pointer" in the larger sense. For example if I have int a = 10; is &a a "pointer" in the larger sense ? I think it must be because I can do pointer arithmetics like (&a) + 1 . What do you think ? – alessio solari Jul 27 '23 at 18:20
  • The following sequence is valid: `int arr[10];`, then `int *ptr1 = arr;`, then `int (*ptr2)[10] = &arr;` – Tom Karzes Jul 27 '23 at 18:20
  • @alessiosolari If you have `int a;` then `&a` is a pointer with type `int *`. Any time you apply the unary `&` operator to something, the result is always a pointer (as long as it's legal). – Tom Karzes Jul 27 '23 at 18:20
  • The two options you give for language describing this in 1) are not substantially different. The "identifier" is a compile-time object and the specification defines the program's semantics. That's the only reason for the difference in language. 2) and 3) are correct, it is a pointer in every way. – BadZen Jul 27 '23 at 18:22
  • Tom Karzes, then why books say that " a pointer is a variable that holds the address of another variable" ? Here &a is not a variable at all and still it is a pointer... – alessio solari Jul 27 '23 at 18:23
  • 1
    The pointer `&a` holds the address of the variable `a`. Keep in mind that this is a semantic description, the compiler is free to choose to never actually store that value in memory, and will probably opt to not do so in many cases. You may be confusing the /variable/ that "a" signifies, and the /value/ that "a" signifies. The variable is technically a compile-time object only. – BadZen Jul 27 '23 at 18:25
  • BadZen, so is it correct to say that an array identifier in C is converted to a pointer to the first element of the array and has as its type the type of the array ? – alessio solari Jul 27 '23 at 18:30
  • @alessiosolari You aren't paying attention to what people are telling you. If `v` is a variable of type `T`, then `&v` is a pionter with type `T *`. That is true for both arrays and non-arrays. Additionally, for arrays only, if the array has type `T arr[N]`, then it is converted to a pointer with type `T *` in various contexts, such as passing it as an argument to a function. But `&arr` still has type `T (*)[N]`, which is consistent since the array itself has type `T [N]`. That's a complete explanation. – Tom Karzes Jul 27 '23 at 18:34
  • 1
    @alessiosolari People often say an array "decays" to a pointer to its first element in certain contexts, although I don't think that term is present in the C standard. But in other contexts, the array and a pointer to its first element behave differently, such as when `sizeof` is applied. – Tom Karzes Jul 27 '23 at 18:38
  • I suggest to @alessiosolari this pretty funny article I found several years ago https://blogs.oracle.com/linux/post/the-ksplice-pointer-challenge – Kalendistrien Jul 27 '23 at 19:07
  • @TedKleinBergman *yes, it is a pointer* Oh? Given `int a[10];` and `int x`, what happens when you write `a = &x`? `a` is **not** a "pointer". Arrays **have** addresses, pointers are variables that **hold** addresses. – Andrew Henle Jul 27 '23 at 19:14
  • @AndrewHenle No, if you define it as an array, then it's not a pointer. I wasn't talking about arrays, I was addressing point 2. While semantically, `&a` is the address of `a` and `int* b = &a` is the pointer to `a`, in all practical cases the expression `&a` can be used instead of `b`. As OP addressed, *"even if a pointer is technically a variable that holds the address of another variable"*, I didn't complicate it further and answered point 3 (that `((&a) + 1)` is doing pointer arithmetic), exactly as `b + 1`. – Ted Klein Bergman Jul 27 '23 at 20:18
  • Related/possible duplicate: [What is array to pointer decay?](https://stackoverflow.com/q/1461432/12149471) – Andreas Wenzel Jul 27 '23 at 20:27

1 Answers1

2

“Pointer” and “address” are used somewhat interchangeably. A pointer to X and the address of X are largely the same thing.

When we seek to make fine distinctions, we might distinguish “pointer” as an object in the C language whose value provides a reference to an object or a function and “address” as a value that tells where in memory an object is. (An address may also tell where a function is, or at least where its entry point is, but this is more complicated.) “Pointer” might also be used to refer to an address in association with a specific pointer type.

In this finer sense, an address is the value of a pointer, the same way an integer is the value of an int. Nonetheless, we are often less precise with language and refer to the “the int 3” rather than “the int value 3”.

For the most part, you can treat “a pointer to the first element of the array” and “the address of the first element of the array” as the same thing.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • The main distinction between a pointer and an address is that a pointer has an associated data type, while an address does not. So if you have `int a[10];`, both `&a` and `&a[0]` refer to the same address, but their data types are different, and as a result they behave very differently. – Tom Karzes Jul 27 '23 at 19:08
  • *“Pointer” and “address” are used somewhat interchangeably.* Confusingly so. A pointer per the C standard is a variable that holds an address, that may or may not be valid. All arrays have an address. Conflating address and pointers is IMO both wrong and done way too often, and it's definitely confusing. – Andrew Henle Jul 27 '23 at 19:16
  • @AndrewHenle: Re “A pointer per the C standard is a variable that holds an address”: C 2018 6.3.2.3 1: “A pointer to `void` may be converted to or from a pointer to any object type.” Since the result of a conversion is not an lvalue, the second use of pointer there is not referring to any variable. 6.3.2.3 3 says “… If a null pointer constant is converted to a pointer type, the resulting pointer, called a *null pointer*, is guaranteed to compare unequal to a pointer to any object or function.” That address may be just `&x`, so not a variable. The standard is flexible about use of “pointer.” – Eric Postpischil Jul 27 '23 at 19:23