Understanding Arrays, Pointers, Operators, and Friends in C++

Overview

Arrays are a fixed length list of indexes paired with values. Pointers are the memory addresses of variables. Operators tell the C++ compiler to manipulate values in a specific way. Class Friends have access to private and protected attributes of other Classes.

What are Arrays

In C++, Arrays are a collection of index value pairs with a fixed length. You access values by selecting a valid index. Indexes in C++ Arrays begin at zero.

string groceryList = [5];
groceryList[0] = “Tomatoes”;
// Another way to intialize Arrays
string groceryList[] = { "Tomatoes", "Potatoes", "Carrots" };
// This errors out, because index is out of bounds of Array
string groceryList[2] = {};
cout << groceryList[4] << endl;

What are Pointers

A Pointer is an operator and variable that contains the address of another variable value stored in memory. It’s like a shortcut on your computer desktop that points to a file located elsewhere.

string message = “Tacos are delicious”;// * operator creates a shortcut to the value
string *shortcut;
// & operator gets the memory location of a variable
shortcut = &message;

// Returns memory address of 0x738d1e8a2370
cout << shortcut << endl;
// Returns Tacos are delicious
cout << *shortcut << endl;

What are Operators

Operators are symbols that tell the C++ compiler to perform specific manipulations on a value. There are various types of operators, such as Arithmetic, Logical, Relational, and Assignment. This is not an exhaustive list, but you can look them up on cppreference.com

Examples of Operators

Arithmetic
+ Addition
- Subtraction
* Multiply
/ Divide

What are Friends

Friends in C++ are Classes that have access to private and protected attributes of other Classes.

class PersonOne {
friend class PersonTwo;
...
}

Key Takeaways

Friend Classes means another Class has access to it’s private and protected attributes. An Array is a fixed length collection of index value pairs. A Pointer is a variable that contains the memory address of another variable. Operators tell the C++ compiler to manipulate values in a specific way.

--

--

I am working toward a simpler, more meaningful world. kristofferhebert.com

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kristoffer Hebert

Kristoffer Hebert

I am working toward a simpler, more meaningful world. kristofferhebert.com