# Source CodeNeed some pointer training ? ;)

#### GAFO666

##### Hacker
Meme Tier VIP
-->Hey Guys, have fun with that, made it actualy for new students on my University but still helpul for learning<--

Part 1: [PointersAndArrays]

C++:
``````/* Made by GAFO666 */

//=========================================== INCLUDES ===========================================//
#include <Windows.h>
#include <iostream>

using namespace std;

//=========================================== mDEFINES ===========================================//

#define MSTART int main(void){
#define MEND return 0;}
#define MPAUSE system("pause");
#define cCOL system("color 8A");
#define cTIT system("title 2D-Arrays und Pointer by GAFO666");

//=========================================== DECLARES ===========================================//

#define MAX 3

//=========================================== Function-Prot's ===================================//
void printMatrix(int Arr[MAX][MAX]);
void initMatrix(int Arr[MAX][MAX]);

//============================================ MAIN ============================================//

MSTART
cCOL
cTIT

int myArr2D[MAX][MAX];
int *pArr[MAX]			= { myArr2D[0], myArr2D[1], myArr2D[2] };
int(*pArr2)[MAX]		= myArr2D;
int *p					= myArr2D[0];

initMatrix(myArr2D);
printMatrix(myArr2D);

cout << endl << "Direct Array Output: \n" << endl;

cout << "\tmyArr2D[1][2]                   = " << myArr2D[1][2] << endl << endl;
cout << "\t*(*(myArr2D + 1) + 2)           = " << *(*(myArr2D + 1) + 2) << endl << endl;
cout << "\t*(myArr2D[1] + 2)               = " << *(myArr2D[1] + 2) << endl << endl;
cout << "\t(*(myArr2D + 1))[2]             = " << (*(myArr2D + 1))[2] << endl << endl;

cout << endl;

cout << "\t*myArr2D[0]                     = " << *myArr2D[0] << endl << endl;
cout << "\t*myArr2D[1]                     = " << *myArr2D[1] << endl << endl;
cout << "\t*myArr2D[2]                     = " << *myArr2D[2] << endl << endl;

cout << endl;

cout << "\tmyArr2D[0]                      = " << *myArr2D[0] << endl << endl;
cout << "\tmyArr2D[1]                      = " << *myArr2D[1] << endl << endl;
cout << "\tmyArr2D[2]                      = " << *myArr2D[2] << endl << endl;

cout << endl;

cout << endl << "Array Output through Pointer-Array: [ int *pArr[" << MAX <<"]; ]\n" << endl;

printMatrix(myArr2D);

cout << endl << "\tint *pArr[" << MAX << "] = { myArr2D[0], myArr2D[1], myArr2D[2] };\n" << endl;

cout << "\t*pArr[0]                       = " << *pArr[0] << endl << endl;
cout << "\t*pArr[1]                       = " << *pArr[1] << endl << endl;
cout << "\t*pArr[2]                       = " << *pArr[2] << endl << endl;

cout << endl;

cout << "\t*pArr[0]+ 1                    = " << *pArr[0] + 1 << endl << endl;
cout << "\t*pArr[1]+ 1                    = " << *pArr[1] + 1 << endl << endl;
cout << "\t*pArr[2]+ 1                    = " << *pArr[2] + 1 << endl << endl;

cout << endl;

cout << endl << "Array Output through Pointer-Array: [ int(*pArr2)[" << MAX << "]; ]\n" << endl;

printMatrix(myArr2D);

cout << endl << "\tint(*pArr2)[" << MAX << "] = myArr2D;\n" << endl;

cout << "\t*pArr2[0]                       = " << *pArr2[0] << endl << endl;
cout << "\t*pArr2[1]                       = " << *pArr2[1] << endl << endl;
cout << "\t*pArr2[2]                       = " << *pArr2[2] << endl << endl;

cout << endl;

cout << "\t*pArr2[0]+ 1                    = " << *pArr2[0] + 1 << endl << endl;
cout << "\t*pArr2[1]+ 1                    = " << *pArr2[1] + 1 << endl << endl;
cout << "\t*pArr2[2]+ 1                    = " << *pArr2[2] + 1 << endl << endl;

cout << endl;

cout << endl << "Array Output through Pointer: [ int *p; ]\n" << endl;

printMatrix(myArr2D);

cout << endl << "\tint *p = myArr2D[0];\n" << endl;

cout << "\tp[0]                            = " << p[0] << endl << endl;
cout << "\tp[1]                            = " << p[1] << endl << endl;
cout << "\tp[2]                            = " << p[2] << endl << endl;

cout << "(This type points always on 1 seted line)" << endl;

cout << endl;
MPAUSE
MEND

//=========================================== Functions =======================================//
void printMatrix(int Arr[MAX][MAX])
{
cout << "Given Matrix with " << MAX*MAX << " Elements: \n" << endl;

for (int s = 0; s < MAX; s++)
{
if (s == 0)
cout << "\tRow: \t   ";
if (s < 10)
cout << "[0" << s << "]";
else
cout << "["<< s << "]";
}

cout << endl << endl;

for (int i = 0; i < MAX; i++)
{
if (i < 10)
cout << "\Line [ 0" << i << " ]  <==>  ";
else
cout << "\Line [ " << i << " ]  <==>   ";

for (int j = 0; j < MAX; j++)
{
if (Arr[i][j] < 10)
cout << "0" << Arr[i][j] << "  ";
else
cout << Arr[i][j] << "  ";
}
cout << endl;
}
}

void initMatrix(int Arr[MAX][MAX])
{
for (int i = 0, c = 1; i < MAX; i++)
for (int j = 0; j < MAX; j++)
Arr[i][j] = c++;
}``````
OutPut:

Notice, In a real project, you would never do e.g.
C++:
``void initMatrix(int Arr[MAX][MAX])``
when you use different sizes etc
so I would recommend in that case
C++:
``````void initMatrix(int **retPT, int x, int y)
{
for (int i = 0, c = 1; i < x; i++)
for (int j = 0; j < y; j++)
Arr[i][j] = c++;
}``````

Part 2: [DynamicArraysAndPointers]

C++:
``````#include <Windows.h>
#include <iostream>

using namespace std;

int ** createArray(int x, int y);
void fillArray(int **retPT, int x, int y);
void printArray(int **retPT, int x, int y);

int main()
{
int ** myArray = NULL;
int dim1, dim2;

cout << "Enter the first Dim of your 2D Array:" << endl;
cin >> dim1;

cout << "Enter the second Dim of your 2D Array:" << endl;
cin >> dim2;

myArray = createArray(dim1, dim2);
fillArray(myArray, dim1, dim2);
printArray(myArray, dim1, dim2);

system("pause");
delete [] myArray;
return 0;
}

int ** createArray(int x, int y)
{
int ** retPT = new int* [x];

for (int i = 0; i < x; i++)
{
retPT[i] = new int[y];
}

return retPT;
}
void fillArray(int **retPT, int x, int y)
{
for (int i = 0, c = 0; i < x; i++)
for (int j = 0; j < y; j++)
{
retPT[i][j] = c;
c++;
}

}
void printArray(int **retPT, int x, int y)
{
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
cout << "retPT[" << i << "][" << j << "] = " << retPT[i][j] << endl;
}``````
Part 3: [NodeManagementWithPointers]

C++:
``````/* Credits: Made by GAFO666 */
//=========================================== INCLUDES ===========================================//
#include "windows.h"
#include <iostream>

//=========================================== mDEFINES ===========================================//
#define MSTART int main(){
#define MEND return 0; }
#define MPAUSE system("pause");
#define cCOL system("color 8A");
#define cTIT system("title doubly linked Lists by GAFO666");

//=========================================== Usings ===========================================//
using namespace std;

//=========================================== DECLARES ===========================================//

struct Node
{
char* name;
int age;
Node * next;
Node * prev;
};

Node * const nPtr = NULL;
Node * const start = (Node*) malloc(sizeof(Node));

Node * tail = start;
Node * actual = start;

//=========================================== Function-Prot's ===================================//
void _addMember(char * Name, int Age);

void _addMemberBefore(char * Before, char * Name, int Age);

void _addMemberBehind(char * Behind, char * Name, int Age);

void _deleteMember(char * Name);

void _setStartParam();

void _printData(void);

void _printDataRev(void);

//============================================ MAIN ============================================//
MSTART
cCOL
cTIT
_setStartParam();

cout << "Normal:\n" << endl;
_printData();
cout << endl << "Fliped:\n" << endl;
_printDataRev();
cout << endl;

cout << endl << endl << "Added GAFO at the end of list: ";
cout << endl;
cout << "Normal:\n" << endl;
_printData();
cout << endl << "Fliped:\n" << endl;
_printDataRev();

cout << endl << endl << "Member Lisa deleted: " << endl;
_deleteMember("Lisa");
cout << endl;
cout << "Normal:\n" << endl;
_printData();
cout << endl << "Fliped:\n" << endl;
_printDataRev();

cout << "\nAdded Jessica before Hans." << endl << endl;
cout << endl;
cout << "Normal:\n" << endl;
_printData();
cout << endl << "Fliped:\n" << endl;
_printDataRev();

cout << "\nAdded Kurt after Jessica." << endl << endl;
cout << endl;
cout << "Normal:\n" << endl;
_printData();
cout << endl << "Fliped:\n" << endl;
_printDataRev();

cout << endl;
cout << endl;
MPAUSE
MEND

//=========================================== Functions =======================================//
void _addMember(char * Name, int Age)
{
actual = tail;

actual->name = Name;
actual->age = Age;
actual->next = new Node;

if (actual->next == NULL)
cerr << "Out of Memory" << endl;

actual->next->prev = actual;
// aktuelle Element gesetzt
actual = actual->next;
tail = actual;
actual->next = nPtr;

}

void _addMemberBefore(char * Before, char * Name, int Age)
{
actual = start;
while (actual->next->name != Before)
{
actual = actual->next;
}

Node * Insert = new Node;

if (Insert == NULL)
cerr << "Out of Memory" << endl;

Node * Temp = actual->next;
Insert->name = Name;
Insert->age = Age;

actual->next = Insert;
Insert->prev = actual;

Insert->next = Temp;
Temp->prev = Insert;
}

void _addMemberBehind(char * Behind, char * Name, int Age)
{
actual = start;
while (actual->name != Behind)
{
actual = actual->next;
}

Node * Insert = new Node;

if (Insert == NULL)
cerr << "Out of Memory" << endl;

Node * Temp = actual->next;
Insert->name = Name;
Insert->age = Age;

actual->next = Insert;
Insert->prev = actual;

Insert->next = Temp;
Temp->prev = Insert;
}

void _deleteMember(char * Name)
{
actual = start;
while (actual->next->name != Name)
{
actual = actual->next;
}

Node * delNode = actual->next;
actual->next = actual->next->next;
actual->next->prev = actual;

free(delNode);
}

void _setStartParam()
{
start->prev = nPtr;

}

void _printData(void)
{
actual = start;
while (actual->next != NULL)
{
cout << actual->name << " is " << actual->age << " years old." << endl;
actual = actual->next;
}
}

void _printDataRev(void)
{
actual = tail->prev;
while (1)
{
cout << actual->name << " is " << actual->age << " years old." << endl;

if (actual->prev != NULL)
actual = actual->prev;
else
break;
}
}``````
OutPut:

Hope those easy exambles help some people to work better with pointers

,greez

#### Solaire

##### Respected Hacker
Dank Tier VIP
Thanks for sharing, I'll take a look at it now

#### GAFO666

##### Hacker
Meme Tier VIP
Thanks for sharing, I'll take a look at it now
take your time man, for known coders its easy, I just showed different variations of accessing and a bit more advanced list

Attention! Before you post:

99% of questions are answered in the Beginner's Guide, do it before asking a question.

No Hack Requests. Post in the correct section.  Search the forum first. Read the rules.

How to make a good post:

• Fill out the form correctly
• Tell us the game name & coding language