When a computer runs, it executes a program that has an address, which is typically stored in the computer’s memory. A pointer is a variable that serves as a reference to another variable, allowing you to manipulate the data stored at that address. Each variable occupies a unique address in memory, which can be used by other variables to yield the same output. There are two key aspects of pointers: the address and the value. Here’s an example of using pointers:
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
int g_int_example = 10;
intmain() { printf("The value of G = %i\n", g_int_example); // Prints the value printf("The memory address of G = %i\n", &g_int_example); // Prints the memory address of g_int_example
cout << endl;
int *l_int_holder = &g_int_example; // Declaration of pointer variable l_int_holder to hold the memory address of g_int_example
printf("The memory address held by L = %i\n", l_int_holder); // Prints the memory address of g_int_example printf("The original memory address of L = %i\n", &l_int_holder); // Prints the actual memory of L printf("The value of L = %i\n", *l_int_holder); // Prints the value of g_int_example cout << endl;
int *l_int_holder2; l_int_holder2 = l_int_holder; printf("The memory address held by L2 = %i\n", l_int_holder2); // Prints the memory address of g_int_example printf("The original memory address of L2 = %i\n", &l_int_holder2); // Prints the actual memory of L2 printf("The value of L2 = %i\n", *l_int_holder2); // Prints the value of g_int_example return0; }
Structure (Struct)
A struct is a data type that contains multiple data elements and allows for different data types. Each variable within a struct has a unique memory address. Structs are used when you have a collection of data of different types, such as user data that includes username (string), password (string), and id (int). To declare a struct, you can use the keyword struct followed by the name of the struct. Here’s an example:
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
structPersonalData {// Declaration of struct named PersonalData string name; // Member of struct (string type variable) named name string origin; // Member of struct (string type variable) named origin string school; // Member of struct (string type variable) named school };
structPersonalDatag_struct_me;// Creating variable g_struct_me to hold the PersonalData struct
intmain() { cout << "Personal Data Structure" << endl;
// All members can have values because each member has a different memory address g_struct_me.name = "Azhar"; g_struct_me.origin = "Bintaro"; g_struct_me.school = "Binus"; cout << "My name is " << g_struct_me.name << ", I am from " << g_struct_me.origin << ", and I teach at " << g_struct_me.school << endl; }
Union
A union is a data type used when there are two or more options for data storage. Although it can hold multiple data types, a union stores them in a single memory address (each member of the union shares the same memory address). Unions are commonly used when there are options to select from in a program. A simple analogy would be in a game, where a player can choose one weapon (knife, pistol, or rifle). A player can only select one of the three options at a time, which is where a union is typically used. To declare a union, use the keyword union followed by the name of the union. Here’s an example:
#include<stdio.h> #include<iostream> #include<math.h> using namespace std;
#define KNIFE 1 #define PISTOL 2 #define RIFLE 3
unionWeaponDamage {// Declaration of union named WeaponDamage (total damage for each weapon) int knife; // Member of union (int type variable) named knife int pistol; // Member of union (int type variable) named pistol int rifle; // Member of union (int type variable) named rifle };
unionWeaponDamageg_un_weaponDamage;// Creating variable g_un_weaponDamage to hold the WeaponDamage union
intmain() { int userActivatedWeapon = KNIFE;
// Only one member can hold a value at a time (because they share the same memory address) if (userActivatedWeapon == KNIFE) { g_un_weaponDamage.knife = 5; } elseif (userActivatedWeapon == PISTOL) { g_un_weaponDamage.pistol = 10; } elseif (userActivatedWeapon == RIFLE) { g_un_weaponDamage.rifle = 20; } else { /* Do Nothing */ } cout << "Union Total Damage for Each Weapon" << endl;
// The results will reflect the last value set in the conditional statement cout << "Total Damage of Knife: " << g_un_weaponDamage.knife << endl; cout << "Total Damage of Pistol: " << g_un_weaponDamage.pistol << endl; cout << "Total Damage of Rifle: " << g_un_weaponDamage.rifle << endl; }
Typedef
Typedef is a keyword in C/C++ used to redefine variables, functions, or data types. Here are examples of how to use typedef:
// Example of redefining a function name typedeffg_int_tambah(a, b)fg_int_functionAdd(a, b);
// Example of redefining a fake function to become an indexed variable typedeffg_int_listName(a) g_int_name[a];
// Example of redefining a struct to be a data type
// Before using typedef structPersonalData { string name; string origin; string school; };
structPersonalDatag_struct_me;// Must use the keyword struct to use PersonalData
// After using typedef typedefstruct { string name; string origin; string school; } PersonalData;
PersonalData g_struct_me; // No need to use the struct keyword again, as PersonalData has been redefined to represent the struct keyword
Indexed Structure
Indexed structures are an advanced concept where a structure can have an index, usually used for large or massive datasets. Here’s an example of creating an indexed struct and populating its values:
intmain() { int index = 0; // Input any index cout << g_struct_login[index].email << endl; cout << g_struct_login[index].username << endl; cout << g_struct_login[index].password << endl; return0; }
Exercise
Create a login system by implementing an indexed struct with a maximum of 10 indices, where there are two menu options for login using email/username. If the email/username or password is incorrect, the program should loop back. If the email/username and password are correct, a login success message should appear. Implement object-oriented programming by creating functions for input and functions for validating the email/username and password. Here’s the expected output:
1 2 3 4 5 6 7 8 9 10 11 12
----------------------- | Azhar Rizki Zulma | | Bintaro | ----------------------- | Basic Login Systems | ----------------------- Enter Email/Username: test (Input by User) Enter Password: gagal (Input by User) Email/Username or Password Wrong, Please try again Enter Email/Username: Moderator (Input by User) Enter Password: momod (Input by User) Login Successful