#include <stdio.h>
int main (int argc, char * argv[])
{
int var = 10;
int *vp = &var;
printf("The value of pointer: %x\n", vp);
printf("The value before: %d, %d\n", var, *vp);
var = 20;
printf("The value after: %d, %d\n", var, *vp);
*vp = 30;
printf("The value then: %d, %d\n", var, *vp);
return 0;
}
int i = 42;
int *pi = &i;
int **ppi = π
#include <iostream>
using namespace std;
/* POINTERS ARE ordinary variables that can store addresses of variables */
int main()
{
int var = 5;
int a = 20;
cout << *&var << endl;
int * const p = &var; //that asterisk here is just used to INFORM about that this variable is a POINTER so it is a variable that can point to address of another variable
// p = &var;
cout << *p << endl; //that asterisk here is used to RETRIEVE (GET) value FROM indicated (pointed) area in our memory (address)
//*p = 20;
// var = 60;
*p = 60;
cout << "var: " << var << endl;
cout << "*p: "<< *p << endl;
cout << "a: "<< a << endl;
int * const p_const = &a; //this is a pointer that has to be initialized when defined, because it cannot change after defining the thing that it is pointing to (address)
const int * p_2 = &a; //this is a pointer that cannot change the value that is under address its pointing to.
const int * const p_3 = &a; //this is a pointer that cannot change the value that is under address its pointing to and also it cant change the address
cout << endl << endl << endl;
int ordinary_var = 10;
int *ordinary_p = &ordinary_var;
cout << "ordinary_var: " << ordinary_var << endl; //integer value
cout << "&ordinary_var: " << &ordinary_var << endl; //integer value
cout << "ordinary_p: " << ordinary_p << endl; //address
cout << "*ordinary_p: " << *ordinary_p << endl; //integer value from pointed place (ordinary_var)
cout << "&ordinary_p: " << &ordinary_p << endl; //address of pointer itself
int ** p_pointing_to_address_of_pointer = &ordinary_p;
cout << "p_pointing_to_address_of_pointer: " << p_pointing_to_address_of_pointer << endl;
return 0;
}
// C++ code for illustration of
// basic_string::c_str function
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main()
{
// declare a example string
string s1 = "GeeksForGeeks";
// check if the size of the string is same as the
// size of character pointer given by c_str
if (s1.size() == strlen(s1.c_str())) {
cout << "s1.size is equal to strlen(s1.c_str()) " << endl;
}
else {
cout << "s1.size is not equal to strlen(s1.c_str())" << endl;
}
// print the string
printf("%s \n", s1.c_str());
}
// C++ code for illustration of
// basic_string::c_str function
#include <bits/stdc++.h>
#include <string>
using namespace std;
int main()
{
// declare a example string
string s1 = "Aditya";
// print the characters of the string
for (int i = 0; i < s1.length(); i++) {
cout << "The " << i + 1 << "th character of string " << s1
<< " is " << s1.c_str()[i] << endl;
}
}
#include <iostream>
using namespace std;
int main()
{
string text = "this a test"; // t r a l a \0
/*
for (int i = 0; i < text.length(); i++)
{
cout << text[i] << endl;
}
*/
/* char ch = text[0];
// cout << ch << endl;
char characters[] = "123asdfasdfasdf4";
cout << characters[0] << endl;
cout << *(characters) << endl;
cout << characters[1] << endl;
cout << *(characters+1) << endl;
char *p = characters;
cout << p[0] << endl;
cout << *(p) << endl;
cout << p[1] << endl;
cout << *(p+1) << endl;
*/
const char * text2 = text.c_str();
cout << text2 << endl;
char array[] = "here is a text";
string test = array;
cout << test << endl;
const char * a = "this is a test 12512412";
cout << a[0] << endl;
char b[] = "this a test 124124";
b[0] ='g';
cout << b << endl;
char * const dynamic_array = new char[50];
dynamic_array[0] = 'k';
dynamic_array[1] = '\0';
//dynamic_array = "lalala";
cout << dynamic_array << endl;
delete [] dynamic_array;
string array_of_string[5] = "this i a text that will be in all of the elements of strings";
array_of_string[0] = "afsdf";
cout << array_of_string[0] << endl;
cout << array_of_string[1] << endl;
cout << array_of_string[2] << endl;
return 0;
}
#include <iostream>
using namespaec std;
class Position
{
public:
Position() // 생성자
{
count << 2) new에 의해 생성자 호출됨 << endl;
}
~Position()
{
count << 3) Delete에 의해 소멸자 호출 됨 <<endl;
}
};
int main()
{
Position* prt = new Position();
count << 1) new 메모리 할당 끝 : 생성자 콜 << endl;
delete(prt); // 메모리 해제
return 0;
}
#include <iostream>
using namespace std;
/* dynamic allocation of memory */
int main()
{
// int var = 41240;
/*
int amount;
cout << "How many numbers would you like to store in an array? " << endl;
cin >> amount;
int *p = new (nothrow) int[amount];
if (p != NULL)
{
for (int i = 0; i < amount; i++)
{
cout << "Enter the " << (i+1) << " number: " << endl;
cin >> p[i];
}
for (int i = 0; i < amount; i++)
{
cout << "p [ " << i << " ] = " << p[i] << endl;
}
}
else
cout << "Not enough memory " << endl;
*/
{
int *p = new int;
cout << p << endl;
delete p;
p = new int;
cout << p << endl;
delete p;
}
//cout << "var: " << var << endl;
//delete []p;
return 0;
}
#include <iostream>
using namespace std;
//void multiplyBy(int &, int);
int *multiplyBy(int *, int);
void multiplyArrayBy(int *, int, int);
int main()
{
int a = 10;
int *b = multiplyBy(&a, 5);
*b = 999;
cout << a << endl;
cout << *b << endl;
int array[10];
cout << sizeof(array)/sizeof(array[0]) << endl;
// 한 어레이에 4바이트 이니까 총 40바이트 하나당 4바이트를 나누면 10 개
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++)
{
array[i] = i;
// cout << "array [" << i << "] = " << array[i] << endl;
}
multiplyArrayBy(&array[0], 5, sizeof(array)/sizeof(array[0])); // &array[0] == array
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++)
{
cout << "array [" << i << "] = " << array[i] << endl;
}
return 0;
}
int *multiplyBy(int * var, int amount)
{
//int * var = &a;
*var = *var * amount;
return var;
}
void multiplyArrayBy(int *array, int amount, int sizeOfArray)
{
while(sizeOfArray--)
array[sizeOfArray] *= amount;
}
#include <iostream>
#include <time.h>
using namespace std;
/* LOTTERY - pseudo-random numbers generator - drawing numbers */
void lottery(int, int);
int main()
{
// srand(time(NULL)); //seeds
// int nr = rand() % 49 + 1; //[1, 49]
// cout << nr << endl;
lottery(49, 6);
return 0;
}
void lottery(int total_balls, int balls_to_draw)
{
if (total_balls < balls_to_draw)
return;
srand(time(NULL)); //seeds
int *balls = new int[balls_to_draw];
for (int i = 0; i < balls_to_draw; i++)
{
balls[i] = rand() % total_balls + 1;
for (int j = 0; j < i + 1; j++)
{
if(balls[i] == balls[j] && i != j)
{
i--;
break;
}
else if (j == i)
cout << balls[i] << endl;
}
}
delete[] balls;
}
#include <iostream>
using namespace std;
int main()
{
int a[3];
a[0] = 0;
a[1] = 20;
a[2] = 40;
short int zm;
int *const p = &a[0];
cout << p << endl;
cout << a << endl;
cout << endl << endl << endl;
cout << &a[0] << endl;
cout << &a[1] << endl;
cout << &a[2] << endl;
cout << endl << endl << endl;
cout << a << endl;
cout << a + 1 << endl;
cout << a + 2 << endl;
//this is kind of same as array a[0],a[1].a[2]
cout << endl << endl << endl;
cout << *a << endl;
cout << *(a + 1) << endl;
cout << *(a + 2) << endl;
cout << endl << endl << endl;
cout << a[0] << endl;
cout << a[1] << endl;
cout << a[2] << endl;
// cout << a++ << endl; // a++ a = a + 1
int * p2 = &a[0];
cout << *p2 << endl; // 0
cout << ++*p2 << endl; //1
cout << *++p2 << endl; //20
cout << *p2++ << endl; //20
cout << *p2 << endl; //40
return 0;
}
#include <iostream>
using namespace std;
/* TYPE CASTING */
int main()
{
double var = 5.6;
int a = 5;
int b = 7;
cout << (double)a / b << endl;
cout << (int) var << endl; //explict way C
int x = 444;
short y = x; //implicit way of casting
cout << y << endl;
cout << int(var) << endl; //explict way C
cout << static_cast<int>(var) << endl; //explict way in C++
char ch;
cin >> ch;
cout << (int)ch << endl;
return 0;
}