Arrays as Pointers in C++

Arrays as Pointers in C++

In C++, arrays and pointers are inextricably linked. The elements of an array are stored in consecutive memory addresses on the computer and the name of the array is equivalent to the address of the first element in it. Because of these facts, it’s easy to use pointers instead of subscripts to read and write the values of an array. To illustrate this point, consider the following program which copies the elements from one array into another and uses pointer notation throughout in place of subscripts:

#include "stdafx.h"#include using namespace std;void print (int* a, int* b, int arraySize) {   int* aPtr;   int* bPtr;   int i;   cout << "The values:" << endl;   for (i = 0, aPtr = a, bPtr = b; i < arraySize;       cout << "A[" << i << "]: " << *(aPtr++) << endl,       cout << "B[" << i << "]: " << *(bPtr++) << endl, i++);}int main() {   const int arraySize = 10;   int* a = new int[arraySize];   int* b = new int[arraySize];   int* aPtr;   int* bPtr;   int i;   // initialise   for (i = 0, aPtr = a, bPtr = b; i < arraySize; *(aPtr++) = 1, *(bPtr++) = 2, i++);   // print   print(a, b, arraySize);   // copy   for (i = 0, aPtr = a, bPtr = b; i < arraySize; *(aPtr++) = *(bPtr++), i++);   // print   print(a, b, arraySize);}
Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular