What exactly is the relationship between pointers and arrays in C++?

360 views

Like, why is it that sometimes, when a function input is supposed to be a pointer, you can use an array instead?

In: 0

3 Answers

Anonymous 0 Comments

Arrays are pointers.

RAM/memory consists of a bunch of “cells” where you can put bytes. Each cell has its own address, and the CPU retrieves this memory by sending the address to the RAM and waiting for a response.

Pointers are of course these memory addresses.

Arrays are consecutively stored elements in memory, and the program merely keeps track of the address of the first element, then adds to this address as it necessary to query the array. That is if the beginning of the array has address 5, I can query the second element of the array by querying address 5+1 = 6.

The way this actually works of course in C is by arrays simply being pointers. Doing arr[i] is equivelent to doing *(arr + i)

You are viewing 1 out of 3 answers, click here to view all answers.