General knowledge help regarding structs, functions and arrays

I’m new to coding, and I just need general knowledge help regarding structs, functions, and arrays, my lecturer is not exactly being very helpful.
Any help will be greatly appreciated.

1 Like

Hey, welcome to the community! @sonicx180

I don’t know a lot about C++, nor about where to learn it, but if you have any specific questions, for example why your code isn’t working, you can post them in #code-help:c!


I’ll just step in here @EarthRulerr can help :smiley:

1 Like

Welcome to the community!

What about structs, functions and arrays do you need clarification on?

For some information, W3Schools is a great resource for beginners and provides simple documentation with examples for many languages.

My C++ is a little rusty so probably double-check everything I say. Also my explanations probably aren’t the best so hopefully my examples or W3Schools will make it clearer.

I’ve just breezed over each topic generally (and pretty terribly to be honest), there is tons to learn about each so specifics would definitely help so that me or someone else can go in-depth on what specifically you want to know/learn.


Structs act as a static object or a fake ‘data type’. You can use them to group together data in structures (hence the name… I’m pretty sure). Structs are created using the struct keyword followed by, optionally a name for the struct, then a pair of curly braces ({}), and optionally the names of variables you’d like to create using the said struct.

// a simple car struct with no name
struct {
	int year;
	string brand;
	char[6] numberplate;
} car;

// using the struct
car.year = 1999;
car.brand = "Toyota";
car.numberplate = { 'A', 'B', 'C', '1', '2', '3' };

// you can create structs from the same struct by separating them with commas
struct {
	int year;
	string brand;
	char[6] numberplate;
} car1, car2, car3;

// this is probably how you will most commonly use structs, it's probably better practice
// you can name your struct to use it as a 'data type'
struct Car {
	int year;
	string brand;
	char[6] numberplate;

// creating and using a variable instantiated from a named struct
Car car;
car.year = 2006;
car.brand = "Hyundai";
car.numberplate = { '1', '2', '3', 'A', 'B', 'C' };


Functions follow the structure return value data-type (void symbolises no return value), function name, opening parenthesis ((), any parameters (just like regular variables, comma (,) separated), closing parenthesis ()), opening curly brace ({), the function body (probably including a return statement somewhere), then the closing curly brace (}).

int multiply(int x, int y) {
	return x * y;
// you can create different versions of the same function using the same name and different data-types (this is called function overloading)
double multiply(double x, double y) {
	return x * y;
// you can call a function by using it's name followed by opening and closing parenthesis with any parameters comma separated in between
multiply(15, 7);     // -> 105
multiply(1.5, 10); // -> 15
// notice how the above both use a different version of the same function!


Arrays are a collection of values of a certain data-type. Arrays in C++ (as in most, if not all, compiled languages) are fixed length (vectors are the go to for a mutable length, array-like object). Arrays are created like a regular variable, however the data type is followed by a pair of square brackets ([]) which can optionally have the length of the array between them, or this can be assumed based on the assigned value. The values you assign to an array are surrounded by curly braces ({}) and are comma (,) separated. You can edit individual values (or elements) of an array using their index, an integer describing their position in the array with 0 representing the first element and so on. You can edit the element at a specified index by assigning to variable[index].

// these are both valid
string[3] names = { "John", "Gregory", "Doe" };
string[] names = { "John", "Gregory", "Doe" };

// changing the first name to "Jonathan"
names[0] = "Jonathan";

std::size(names); // bonus: the number of elements in the array

Matt said it ^^^^^^^

1 Like