1
int main ()
{
  int numbers[5];
  int * p;
  p = numbers;  *p = 10;
  p++;  *p = 20;
........

ok so this is from the pointers section in the c++ manual from their website. I'm lost.

What is it incrementing in line 4 if p is a reference to an array? Or is it even pointing to the array numbers?

When would this ever be better than just reassigning the values in the array?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Tony Shepherd
  • 115
  • 1
  • 8
  • 4
    p is not a reference to an array. p is a pointer to an integer. – Mooing Duck Jun 10 '13 at 19:40
  • That `p` value isn't a reference to the array `numbers`, it's a pointer to an integer. – Tyler Jandreau Jun 10 '13 at 19:40
  • Why have three people said nearly the same thing? – chris Jun 10 '13 at 19:41
  • @chris: Because we all posted it within 50 seconds of each other – Mooing Duck Jun 10 '13 at 19:41
  • 1
    Who are "they" in "their website"? – Cubbi Jun 10 '13 at 19:50
  • 1
    I think this question the author has assumed , a = b to take properties of b entirely to a . I had this problem at some point . One thing that might be worth for you to learn is the concept of lvalue and rvalue , in c a = b will work like In A's Memory Location - Store what B's Evaluated Value is . In Python its different for example . There is no commutative property in C I think , if a = b , b != a . a != b either ...Any comments ? I am just learning . – Nishant Jun 10 '13 at 19:56
  • @Nishant: I barely understood that until you mentioned communtative properties, and then I lost it. In C, if `a==b`, then `b==a`. If `a=b`, then `b==a` and `a==b` both. custom C++ classes can muddy the water a little, but in general, that holds true in C++ as well. – Mooing Duck Jun 10 '13 at 20:01
  • Mooing Duck , I am wondering , if a=b , then b==a and a==b is probably true but ... Say in his example . int* p; int array[10]; p = array; sizeof(array) and sizeof(p) are different no ? I wanted to say both 'objects' are not exactly equal . Is that incorrect statement ? I mean = operator works a bit differently than in Math in C is what I thought . What do u think ? – Nishant Jun 10 '13 at 21:01

4 Answers4

6

p is not a reference to an array, it's a pointer to an integer. When a pointer is incremented, its value is incremented by the size of the pointed-to type, in this case 4.

In the assignment p = numbers;, numbers (the name of an array) decays into a pointer to its first element (ie, &numbers[0]), which is then assigned to p. Then, after you do p++, p points to the next element of numbers — that is p is equal to &numbers[1].

In the case you've shown, there's no reason to perform this operation over just using array access directly. You might need to do it to interact with some function or API, or for some other program design reason, which is why it's possible.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • ooohhh ok so I'm assuming that in an array all the memory locations are sequential and thus it's just moving to the next memory location by incrementing `p` which is also the next element in the array? – Tony Shepherd Jun 10 '13 at 19:42
  • @MooingDuck, that's what the second paragraph says, I think. Or do you mean I should flesh out something else? – Carl Norum Jun 10 '13 at 19:44
  • 2
    @user2050069 the memory elements of the array are guaranteed to be in adjacent memory locations. And the type of the pointer (`int` in this case) sets the size of the steps taken each time the pointer is incremented. – juanchopanza Jun 10 '13 at 19:44
2

p is pointing to the first variable in numbers array so

*p = 10

sets numbers[0] = 10.

Then he increments the address pointed by p (p++) so now p points to numbers[1]. And then it fills numbers[1] with 20. That's all

Cob013
  • 1,057
  • 9
  • 22
1

Here is what I think , please correct me if I am wrong .

int *p; decides what p should be - an integer pointer i.e a variable that stores a memory address location (which expects nothing but an integer value if you deference)

p = numbers; doesn't decide what p should be that is already declared in the earlier line .

It just assigns the variable p (i.e the allocated a memory for p) with the 'rvalue' of numbers . ie number computed value which is actually a memory adress to the first item in the numbers array .

p and number has nothing in common but that they have the same value - which is a memory address . p and number is treated differently by c++ because p points to an integer , number points to an array . if you do a sizeof you will get integer's and array's sizeof as return .

Nishant
  • 20,354
  • 18
  • 69
  • 101
0

In C++, an array is a kind of pointer, one that points to the first value in the array. The syntax numbers[2] means the same thing as *(numbers+2). When you copy the pointer numbers into p, you're making p also point to the first value of the array, numbers[0]. Incrementing it with p++ makes it point to the second value of the array, numbers[1].

When you execute the commands *p = 10; and *p = 20;, you're setting the value pointed to by p equal to 10 and 20; these are numbers[0] and numbers[1] respectively.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
IanPudney
  • 5,941
  • 1
  • 24
  • 39
  • 1
    I think telling novices that "the identifier used to name an array is itself a kind of pointer" is dangerously misleading. – Mooing Duck Jun 10 '13 at 19:51
  • 4
    *"In C++, the identifier used to name an array (in this case, numbers) is itself a kind of pointer"* -- No, it's not. An array is an array. The array's name is how you refer to the array in source code. Say this instead: An array is implicitly convertible to a pointer to its first element. – Benjamin Lindley Jun 10 '13 at 19:52
  • It's still just as wrong though. An array is not a "kind of" pointer, nor is it "kind of" a pointer. – Benjamin Lindley Jun 10 '13 at 19:55
  • An array is very much a pointer. In fact, A[B] and B[A] both point to the same place, because they both mean *(A+B), and addition is commutative. – IanPudney Jun 10 '13 at 19:58
  • You are confusing element access via `[]` with declaration. The type of `numbers` is distinct from that of `p`. – juanchopanza Jun 10 '13 at 20:00
  • 1
    No. Addition and indexing through `operator[]` do not operate on arrays, they operate on pointers. They appear to work on arrays because of what I said in my first comment. "An array is implicitly convertible to a pointer to its first element." Whenever you do an operation on an array that is not defined to work for arrays, the compiler will check if that operation happens to work on a pointer. If it will, then the compiler will do the conversion. – Benjamin Lindley Jun 10 '13 at 20:01
  • Proof: `int arr[10]; int *p; static_assert(sizeof arr == sizeof p, "An array is not a pointer.");` – chris Jun 10 '13 at 20:27
  • Is Array Name a Pointer - http://stackoverflow.com/questions/1641957/is-array-name-a-pointer-in-c – Nishant Jun 10 '13 at 20:57
  • I learned something today. – IanPudney Jun 11 '13 at 11:49