Clilstore Facebook WA Linkedin Email
Login

This is a Clilstore unit. You can link all words to dictionaries.

POINTERS AND WORK WITH DATA STREAM

Working memory of the personal computer stores data in cells. Each of them has its own address. These numbers are called addresses, by which it is possibleto refer to any byte of the memory.

In the program any variable has its name and value. By referring to the variable by name,it is possible to obtain its value. The assignment statement assigns to the variable a value located to the right from the statement. If consider this process on the machine level, then the variable name corresponds to the allocated memory address and the value of the variable - to the content of the allocated memory. Will show the connection between the name of the variable and address.

 

Software level

 

Е-name

VariableValue

_ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _ _ _ __ _ _ _ _ _ _ _ _ _ _

Part of memory

Content

 

Machine level &E- address

 

Consider the relationship between name, address and value of the variable.

char s=’a’;

int x=1895;

float p=2.014e-5;

These variables are allocated in the memory as follows.

 

memory address

1A2B

1A2C

1A2D

1A2E

1A2F

1A30

1A31

 

byte

byte

byte

byte

byte

byte

byte

value in memory

a’

1895

2.014*10-5

variable name

S

X

P

 

The character variable occupies 1 byte of memory, the integer variable - 2 bytes, and the real variable - 4 bytes.

Pointer - is a variable whose value is the address of the memory cell.

& - unary operation of address taking. This operation retrieves the address of the declared variables in order to assign it to the pointer.

Operation &E returns a memory address reserved for the value of the variable E. The address of the first memory cell reserved for the variable shall be taken as the variable address. Addresses of the above variables will have the following meanings:

&s= 1A2B

&x=1A2C

&p=1A2E.

Variables of “pointer”type are used to store addresses. The format of description of such variables is as follows:

type *name_of variable;

Examples of pointers description:

char *k;

int *z;

float *f;

After such description, variable k can take the value of the pointer by the value of character type; variable z is designated to store a pointer bythe value of the integer type; variable f - by the value of float type.

Pointers can be assigned values of addresses of objects only of that type, with which they are described.

For example: k=&s; z=&x; f=&p;

As a result, pointers will take the following values:

k-1A2B, z-1A2C, f-1A2E

As well as for other types of data, values of pointers can be initialized during description.

For example:

char s=’a’; сhar *k=&s;

int x=1895; int *z=&x;

float p=2.014e-5; float *f=&p;

Character * (asterisk) used in description of pointers in this context is the sign of dereferencing operation. With its help, it is possible to refer through the pointer to the corresponding variable.

Using of pointers for transfer of the function parameters

In the following example, using the swap () function, the exchange of values of two variables set by their pointers in the arguments is carried out.

void Swap( int*a, int *b)

{int c;

c=*a; *a=*b; *b=c;

}

The segment of the main program shall be written as follows:

main()

{int x=1,y=2;

Swap(&x, &y);

cout<<”x=”<<x<<”y=”<<y;

}

As a result, it will be displayed on the screen: x=2 y=1, i.e. x and y variables have changed with values.

After call to the function, pointera will get the address of variable x, pointerb - address of variable y. After that, variable x in the main program and dereferenced pointer * ain function are connected with one memory cell, also - variable y, and address * b.

Thus, it is possible to conclude that the use of pointers in function parameters allows to simulate the work of procedures.

Clilstore

Short url:   https://clilstore.eu/cs/6255