Skip to content

E) Pointers basic

kalinosia edited this page Mar 13, 2021 · 2 revisions

INT

int value = 10;
int *pointer = &value;

cout << value << endl << &value << endl;
cout << pointer << endl << *pointer << endl << &pointer << endl ;

OUTPUT:

10 _(value)_
0x7d4a4528dbe8 _(&value)_
0x7d4a4528dbe8 _(pointer)_
10 _(*pointer)_
0x7d4a4528dbe4 _(&pointer)_

IN CHAR & STRINGS THE SAME

and in strings also:

string value = "mata";
string *pointer = &value;
cout<<(*pointer)[2]<<endl; //-->t

TABLE INT

int table[5]={1,2,3,4,5};
int *pointer = table; // point to table not to address
cout<<*pointer<<endl; // ==> cout<<&table<<endl; 
pointer=pointer+2; // USE IN FOR LOOP EXAMPLE <------ ...;i++,pointer++){....
cout << *pointer <<endl; // (table[0]+2=table[2] => 3)
int *point_to_last = &table[2]; // while loop example

TABLE STRING THE SAME and:

string table[3]={"mama", "dama", "kama"};
....
SIZE STRING TABLE
cout<<sizeof(string)<<endl; //size of string = 8
cout<<sizeof(table)<<endl; //size of string x how many in table (3) = 24
cout<<sizeof(table[0])<<endl; //size of first element (string size) = 8
cout<<table[0].size()<<endl; //lenght of first element = 4 (mama) <------

HOW MANY ELEMENTS IN TABLE

int elements = sizeof(table)/sizeof(string); //if string

CHAR TABLE

char text[] = "hello";
int len = sizeof(text)-1; //-1 because last \0
char *pStart = text;
cout<<pStart<<endl; //--> hello
cout<<*pStart<<endl; //-->h
cout<<text<<endl; //-->hello
char *pEnd=text+len-1; //char *pEnd=pStart+len-1;
cout<<*pEnd<<endl; //-->o 
cout<<pEnd<<endl; //-->o 

reversing char table....https://www.udemy.com/course/free-learn-c-tutorial-beginners/learn/lecture/1747862#announcements

Clone this wiki locally