2

im watching this tutorial on youtube https://www.youtube.com/watch?v=8XAQzcJvOHk Dynamically Allocating Arrays Depending on User Input in C++

this is his code

1 int main()
2 {
3   int *pointer = nullptr;
4   
5   cout << "how many items u are gonna enter" << endl;
6   int input;
7   cin >> input;
8
9   pointer = new int[input];
10
11  int temp;
12
13  for (int counter = 0; counter < input; counter++) {
14      cout << "enter the item " << counter + 1 << endl;
15      cin >> temp;
16      *(pointer + counter) = temp;
17  }
18
19  cout << "the items you have entered are" << endl;
20  for (int counter = 0; counter < input; counter++) {
21      cout << counter + 1 << "  item is  " << *(pointer + counter) << endl;
22  }
23
24  delete[]pointer;
25
26  return 0;
27}

im stuck in line 16, i dont understand why is that, inside the (), the pointer variable and counter are added to each other

3
  • 3
    *(pointer + counter) is just another (and wackier) way to write pointer[counter]. It's not clear why the author chose to obfuscate matters in this way. In any case, it would have been better to use std::vector<int> in place of raw pointers and manual memory management. Commented Sep 7, 2016 at 13:49
  • 2
    In real code this should be std::vector. Commented Sep 7, 2016 at 13:58
  • 3
    And while I'm giving unsolicited coding advice: don't define pointer until it's used, i.e., int *pointer = new int[input];; and define temp inside the body of the for loop, since that's the only place it's used. Commented Sep 7, 2016 at 14:01

4 Answers 4

2

Pointer Arithmetic is a good point where to start.

I'm going to try to explain you briefly how it works, but I strongly suggest you to integrate those concepts with a good book or internet references because they are very important for proper handling pointers.

A pointer (as you can imagine from the name) points a memory cell:

int* ptr = /*an address to the memory cell*/

Your memory is composed by sequentially cells, graphically:

Address|      Value
-------|------------
0x00   |    [#some value] 8 bit
0x01   |    [#some value] 8 bit
...    |    ...
0xN    |    [#some value] 8 bit

Just to make this example not so long, we can assume each cell contains 8 bits and a integer value is represented with exactly 32 bit (usually that is not true, and it depends on the machine architecture and compiler).

Then a int value is stored exactly in 4 cells. (We explicitly don't consider memory alignment).

So your pointer contains a memory location, the address in the memory which contains the value you've allocated (with the usage of dynamic memory).

For example:

int* ptr = 0x01

That means the variable ptr, stored somewhere in the memory, contains the address 0x01. In the memory cell 0x01 there will be the integer value allocated dynamically.

But, since the value is an integer type, the data will take 4 cell in order to store the complete information. So the data will be "split" among the cell 0x01, 0x02, 0x03, 0x04.

The pointer will points the first memory location of the data, and the number of cell occupied is given by the type of pointer (in that case pointer int so the compiler knows the information starts from cell 0x01 and ends 0x04).

A variable pointer can be evaluated in an arithmetic expression, such sums and differences.

Fo example:

ptr + 10
ptr - 10 

Simply, the meaning of that expression is to access to memory address starting from the address stored in ptr and jumping 10 int cells forward or backward.

Attention Note: the expression does not mean to simply add the value to the address obtaining a new address.

Indeed, assuming ptr = 0x01, then the expression:

ptr + 10;

does not mean 0x01 + 10 = 0xa!

Instead that means to jump 10 "block" of size equal to the type's size pointed by the pointer itself.

That is, 0x01 + 10 * 4bytes. Since ptr is a pointer to int, then +10 means "plus 10 block of integers", and, in this example, each int occupies 4 bytes (32 bit).


To conclude, the expression:

*(pointer + counter) = temp;

means to access to the address start from pointer and adding #counter block of int, then deference that address with the operator* and write in that address the value temp.

That notation can be easily simplify with the operator[]:

pointer[counter] = temp;

where the meaning is exactly the same, but the notation is more readable, especially when you have to do with array.

Sign up to request clarification or add additional context in comments.

1 Comment

It is odd to start with bits when the smallest addressable memory location is a 'byte'.
1

This part:

*(pointer + counter)

is just simple pointer arithmetic: we are adding counter (of type int) to the pointer address and then dereferencing it using *. It is the same as pointer[counter]. After that, we are savig value of temp into that particular (dereferenced) location in memory.

Comments

1

*(pointer + counter) is equivalent to pointer[counter] as has been pointed out, the reason it's equivalent is because pointer holds a memory address, when you add say 1 to the that memory address you are infact adding the size of whatever the data type that pointer is pointing to is, multiplied by 1.

If you have a primitive array

int arr[2] = {1,55};

*arr would give you 1 and *(arr + 1) would give you 55

Comments

1
  *(pointer + counter) = temp;

is same as

pointer[counter] = temp;

The variable pointer contain the address of the first element of the array. Adding counter means selecting the address of counter away from starting address.

counter is simply an offset from pointer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.