4

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.

Community
  • 1
  • 1
viuser
  • 953
  • 6
  • 19
  • 1
    The data `y` points to isn't `y` (you could assign `y = x;` and `y` would point somewhere else, but it still wouldn't *be* `y`). The data in `x` is actually an intrinsic part of `x`; `x` is that memory, and no other. – ShadowRanger Feb 10 '19 at 01:40

4 Answers4

3

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];
Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
  • Uhm sorry, so in practice, if we don't care about warnings, the difference in your example would be only noticeable if we do things like `ptr = (&arr)+1;` (bad!) ? – viuser Feb 10 '19 at 02:31
2

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.

o11c
  • 15,265
  • 4
  • 50
  • 75
0

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.

-2

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
  • 2
    When you use `malloc` what you have is a pointer to a block of memory. You can change where the pointer points, you can resize it. Cause a memory leak, by pointing it somewhere else. None will happen with an array. Though most people think of it as an array. – Manny_Mar Feb 10 '19 at 02:49
  • Yes, it will cause a memory leak when improperly used. What you'll get are garbage values because you are directly accessing memory address (memory cells in your RAM hardware) if the code has logical errors in it. Which is why it is better not to use pointers as much as possible. – user9512695 Feb 10 '19 at 03:00