viuser
viuser

Reputation: 965

How is a C array a “named variable in its own right”?

On page 103 in the book “Expert C Programming: Deep C Secrets” by Peter Van Der Linden there is a table about the difference between arrays and pointers.

One issue I don't really understand – direct quote:

Pointer: Typically points to anonymous data

Array: Is a named variable in its own right

What does this mean? Since you can do the following:

#include <stdio.h>
#include <stdlib.h>

int main(void){
  int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  int *y = malloc(9*sizeof(int));
  printf("sizeof(x) == %zu\n", sizeof(x));
  printf("&(x[2]) = %p\n", (void*)&(x[2]));
  printf("sizeof(y) == %zu\n", sizeof(y));
  printf("&(y[2]) = %p\n", (void*)&(y[2]));
  return 0;
}

Output:

sizeof(x) == 36
&(x[2]) = 0x7fffffffe5f8
sizeof(y) == 8
&(y[2]) = 0x800e18008

I don't see how y is less of a named variable than x.

Upvotes: 4

Views: 153

Answers (4)

user10762593
user10762593

Reputation:

We're basically guessing what someone else meant. But here's my best guess.

  int x[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  int *y = malloc(9*sizeof(int));

x is the name of an array.

y is the name of a pointer. The array it points to has no name.

Upvotes: 0

Govind Parmar
Govind Parmar

Reputation: 21572

The author could have been more clear here, I agree. A pointer is a named variable in its own right too, but if it points to an array, it doesn't have any information about the length of the array. In fact, it doesn't know that it's pointing to an array. The syntax p[100] is valid (although undefined) even if p was assigned the address of a single int or other data type.

This is why when an array is passed as an argument to a function, it is either:

  • Accompanied with a "length" parameter that trusts the calling code to supply it correctly
  • Terminated with a sentinel value (like the null terminator for strings)

To more clearly demonstrate this distinction, try this:

int arr[3] = { 1,2,3 };
int *ptr;
ptr = &arr;

I get the following compilation warning:

'=': 'int *' differs in levels of indirection from 'int (*)[3]'

But, if you change ptr to point to arr's first element (which is what happens when arr decays to a pointer), there's no problem:

ptr = &arr[0];

Upvotes: 3

user9512695
user9512695

Reputation: 5

Actually, what you wrote are two ways to declare an array. The first one is, of course, the more conventional and easier. What the author is trying to tell us is that an array is a named variable because you need the named variable to access all the elements of the array. To get to the next element and so forth, you need to place the index number. So, the "address" of the first element multiplied by the index number, you will get to the desired element. If we analyze it, an array[0] points to the first element and is a pointer in itself.

To properly understand pointer though, consider this:

int *y = malloc(sizeof(int));
int x[] = {1,2,3};
y = &x[2]; //points to an anonymous data because pointer "y" doesn't "know" the 
           //variable "x", only it's memory address
y = &x[0]; //points to the first element of x

Upvotes: -2

o11c
o11c

Reputation: 16156

I think what the author is trying to say is that an array's elements form a named "object" (by the definition of the C standard), whereas a pointer's elements are often from an unnamed "object".

See C11 §3.15 for the definition of an object, and §6.2.4 for information on an object's storage duration.

There are a lot of crappy programming books out there, and C being an old and popular language, it has more than most.

Upvotes: 2

Related Questions