Slide # 1

Slide # 1

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 2

Slide # 2

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 3

Slide # 3

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 4

Slide # 4

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Slide # 5

Slide # 5

Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts Read More

Friday, November 12, 2010

C++ - Pointer

Pointer

Without doubt, the pointers are the most important in C++, and also in the same time and the most confused. With them do support for joined lists and dynamic memory.

The pointer is variable which contains a memory address. Very often the memory address is location from other variable. General declaration of pointer is:







type *var_name;




type is one from the types of variables in C++. var_name is the name of the pointer. If we want p to be the pointer of variable from the type integer:








int *p;



The variable’s type on which shows the pointer must be marked. Some authors recommends the name of the pointer to start with i if it is for integer variable, with f if it is for float, with d if it is for double, but it isn’t important.

For pointers are used two operators: * and &.

& is operator which gives the memory address, for example p = &i.

* is complement of & and it gives the value in the memory address, respectively that is written in the memory address.


The important is that the pointer has to be from the same type how and the variable on which it will show. Exist and pointer from type void. This pointer can shows on variable from each type, but for using in each manner in the end it has to convert to (cast) analogous type.

With pointer it can be written value in the memory location, for example with the phrase *p = 101, it likes to say on the variable on which shows the pointer p allocate value 101. To enlarge the variable’s value on which the pointer shows, it uses *(p++). Here the brackets are compulsory because the operator * has smaller priority from the operator ++. How result from the previous phrase on the variable on which shows the pointer p it will be allocated value 102.

Exist 4 arithmetic operators which are applied to the pointers and they are:

+,  -,  ++,  --.  With the phrase p++ make move on the pointer to the next memory address. And with - on the previous memory address. We aren’t limited with increase and deduction for 1. It can to write free and p = p + 9, it means that the pointer moves for 9 memory locations to ahead.

Exist a large connection between pointer and rank:









char str[50];

char *p;

p = str;



The pointer is marked on the first element in the rank. That is str[0]. In C++ the name of the rank without any index gives the first element from the rank. It means the phrase p = str is p = &str[0]. This is one from the important things in C++, that the name of the rank without additional parameters gives the first element in the rank. The two next phrases now are identity: str[4] and *(p+4)The both phrases give the fifth element in the rank.

The arithmetic with pointers in a rank knows to be very faster from the indexing on a rank in a lot of cases.

We saw that with the arithmetic of pointers can access to each element of the rank. The amazing is that it can to index and the pointer.

The phrase p[i] is identical to the phrase *(p+i).

Exist and rank of pointers. The phrase int*ipa[10] declares rank from 10 pointers and each one of them shows on total number.

Important: When the pointer is declared, but before to investiture the variable’s address, it contains arbitrary value. If the pointer is used before to be allocated some variable, the chances for mistake are big. To prevent something like this is recommended equaling on null or 0 of the pointer.








float *p = 0;



How invoke function with pointer?


C++ admits to declare pointer like parameter of the function.







#include <iostream>

using namespace std;


void f(int *j);



int main(){

                int i;

                int  *p;

                p = &I;


                f(p); //ili f(&i);

                cout<<i;

                return 0;

}


void f(int *j){

                *j = 100;

}  




With the pointer makes direct change on the devotion argument.

The phrase *j = 100, direct changes the value on the entrance parameter, i.

When a rank is argument of the function, it lets go just the address from the first element of the rank, not the whole rank. With that it’s prevented making copy of the rank. One manner, for example, is:








void display(int num[10]);




When it invokes the function it is enough to insert just the rank’s name without indexing. The second manner is to insert the rank without dimensions:








void display(int num[]);



To remember that when the rank is let like argument in function, it lets the address of the first element in the rank. A cause of it the rank of default lets by reference like argument, it has to be careful that with it the rank is vulnerable, because it suffers all the changes while the function goes on.

The function can to back a pointer. Previous it has to be declared so. For example with the phrase int*f() the function backs a pointer. A cause of it and the value in return has to be a pointer.

0 comments:

Post a Comment