computers and programming the 4 th lecture jiří Šebesta

21
Computers and programming The 4 th lecture Jiří Šebesta

Upload: andrew-wiggins

Post on 03-Jan-2016

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computers and programming The 4 th lecture Jiří Šebesta

Computers and programming

The 4th lecture

Jiří Šebesta

Page 2: Computers and programming The 4 th lecture Jiří Šebesta

TOPIC

1. Pointers

2. Arrays and pointers

3. Array of pointers

4. Examples

Page 3: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (1/10)

• Pointer is a data type determined for storing an address as a variable in the computer memory

• Required memory size for storage of pointer (number of bytes) is given by used memory space, which is implemented in the computer system

• In small microcontroller applications, the pointer needs usually 16 bits (2 B) with memory addressing up to 64 kB (addresses 0x0000 – 0xFFFF)

Page 4: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (2/10)

• In case of application of higher memory space, a segmentation is used. Whole memory space is divided to few segments (pages) with size 64 kB and pointer shows address in given actual segment - near pointer. Far pointer is completed by identifier of memory segment (page), address in this segment is specified by offset address.

Page 5: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (3/10)

• In 32-bit systems 32-bit pointer (4 B) is used. Add-ressing is in range 0x00000000 do 0xFFFFFFFF, mem. space 4 GB.

• In 64 bit systems 64-bit pointer (8 B) is used, mem. Space is 16 HB (hexabytes)

• Notice

1 kB = 210 B = 1024 B

1 MB = 220 B = 1024 kB = 1048576 B

1 GB = 230 B = 1024 MB, 1 TB = 240 B = 1024 GB

1 PB = 250 B = 1024 TB, 1 HB = 260 B = 1024 HB, etc.

Page 6: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (4/10)

• Pointer aritmetics represents computation operations with pointers. Addressing unit can be 1 byte, 1 word (2 B), in C language also size of data type (char – 1 B, int – 4 B, double – 8 B ).

int *a, *b; //pointers to integerint x;int y[5] = {1, 2, 3, 4, 5};a = &x; //pointer a contents address of variable x*a = y[2]; //fill address defined by a by y[2], x=y[2]printf("%d\n", x);b = &(y[3]); //pointer b contents address of var. y[3]*b = *a; //content of address pointed by a is copied

to content of address pointed by bprintf("%d\n", y[3]);

Example: Ex40.c

Page 7: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (5/10)

• Elements of one-dimensional array (vector) are stored in memory subsequently – indexing (to show x-th element) can be done by pointers. int *adr; //pointer to integerint arr[50], i;

adr = &(arr[49]); //pointer is set to the last elementfor(i=0; i<50; i++){ *adr = i; //a number is paste to the address

//defined by pointer adr adr--; //pointer is shifted down (-1 element

//of arr = -4 bytes due to int type}for(i=0; i<50; i++) printf("%d\n", arr[i]);

Example: Ex41.c

Page 8: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (6/10)

• Name of array without index is pointer to the first element of array: entry A[0] is equivalent to *A

entry A[5] is equivalent to *(A+5)int *adr; //pointer to integerint arr[50], i;

adr = arr; //pointer is set to the first elementfor(i=0; i<50; i++){ *adr = i; //a number is paste to the address

//defined by pointer adr adr++; //pointer is shifted up (+1 element

//of arr = +4 bytes due to int type}for(i=0; i<50; i++) printf("%d\n", arr[i]);

Example: Ex42.c

Page 9: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (7/10)

• Ex. Using relation operators for pointersint *a, *b, *c; // pointers to integerint arr[20], i;

a = arr;for(i=0; i<20; i++) {*a = i; a++;}for(i=0; i<20; i++) printf("%3d", arr[i]);b = arr+5;c = arr+15;a = arr;for(i=0; i<20; i++){ if (b<a && c>a) //address a must be between b and c *a=0; a++;}for(i=0; i<20; i++) printf("%3d", arr[i]);

Example: Ex43.c

Page 10: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (8/10)

• Ex. Integer v in memory in bytesint *a; //pointer to intchar *b; //pointer to charint arr[5], i;for(i=0; i<5; i++) arr[i] = i;a=arr;for(i=0; i<5; i++) //int address and value displaying{ printf("%x %3d\n", a, *a); a++;}b=arr;for(i=0; i<20; i++) //char (byte) addr. and value disp.{ printf("%x %3d\n",b, *b); b++;}

Example: Ex44.c

Page 11: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (9/10)

• Ex. Float in memory in bytesunsigned char *a; //pointer to ucharfloat num = 1.2; //float value in memoryint i;

a=&num;for(i=0; i<4; i++) //four bytes of float value in mem.{ printf("%x ", a); //address printf("%x\n", *a); //content a++;}

Example: Ex45.c

• For 1,2 is in memory store value (hex.) 9A 99 99 3F, from the least significant byte to the most significant byte

Page 12: Computers and programming The 4 th lecture Jiří Šebesta

Pointers (10/10)

• 1,2 in float resolution has form

hex: 0x 3 F 9 9 9 9 9 A

binary: 0b0-01111111-00110011001100110011010

82000000476,182000000476,0121

8388608

1677722121

2121

0

127127023

127

M

x ES

• sign: 0 = +• exponent: 0b01111111 = 127• mantissa: 0b00110011001100110011010 = 1677722

Page 13: Computers and programming The 4 th lecture Jiří Šebesta

Arrays and pointers (1/3)

int *a; //pointer to intint arr[4][5], i,j;

for(i=0; i<4; i++){ for(j=0; j<5; j++) { arr[i][j]= 10*i+j; printf("%3d", arr[i][j]); } printf("\n");}a=arr;for(i=0; i<20; i++, a++) //20 elements of array in mem printf("%x %3d\n",a, *a); //printing

Example: Ex46.c

• Two-dimensional array in memory

Page 14: Computers and programming The 4 th lecture Jiří Šebesta

Arrays and pointers (2/3)

• Multi-dimensional array in memory

int *a; //pointer to intint arr[3][3][3], i, j, k;

for(i=0; i<3; i++) for(j=0; j<3; j++) for(k=0; k<3; k++) arr[i][j][k] = 100*i + 10*j + k;

for(i=0; i<27; i++, a++) //27 elements of array 3x3x3 printf("%x %3d\n", a, *a); //in memory printing

Example: Ex47.c

Page 15: Computers and programming The 4 th lecture Jiří Šebesta

Arrays and pointers (3/3)

• Two-dimensional array in memory:

• Equivalent entry:

arr[i][j][k] = *(arr+i*3*3+j*3+k)

Page 16: Computers and programming The 4 th lecture Jiří Šebesta

Array of pointers (1/2)

• Array of pointers application – searching positions of given character in the text char text[] = "okolo kola okolkovala koala";char *o[10], *c; //o is array of pointersint i=0, j;

printf("Original string: %s\n\n", text);printf("Addr. of the beginning of text: %x\n\n", text);

o[i] = strchr(text, 'o');while(o[i]){ c = o[i]+1; i++; o[i] = strchr(c, 'o'); //save addres of ’o’ to

//array of pointers}

Page 17: Computers and programming The 4 th lecture Jiří Šebesta

Array of pointers (2/2)

printf("Addresses of the character o:\n");

for(j=0; j<i; j++) printf("%x - position %d\n", o[j], o[j]-text); for(j=0; o[j]!=0; j++) // changing of all ’o’ by ’k’ *o[j]='k';

printf("\n\nChanged string: %s\n", text);

Example: Ex48.c

Page 18: Computers and programming The 4 th lecture Jiří Šebesta

Examples (1/3)

• Ex. Changing characters by using pointers

char text[] = "okolo kola okolkovala koala";char *a;

printf("Original string: %s\n\n", text);a=text;while(*a != NULL){ if (*a == 'o') *a = 'k'; else if (*a == 'k') *a = 'o'; a++;}printf("Changed string: %s\n", text);

Example: Ex49.c

Page 19: Computers and programming The 4 th lecture Jiří Šebesta

Examples (2/3)

• Ex. Searching and replacement of characters in text without standard functions using pointers char text[]="sokol kolem jezdil kolem dokola";char test[]="kol";char ntext[]="----------";char *a, *b, *c;int tst;

a=text;b=test;printf("Original string: %s\n\n", text);for(a=text; *a!=NULL; a++){ tst = 1; c = a; for(b=test; *b!=NULL; b++,c++) if (*c != *b) tst=0;

Page 20: Computers and programming The 4 th lecture Jiří Šebesta

Examples (3/3)

if(tst) { c = ntext; for(b=test; *b!=NULL; b++,c++,a++) { *a = *c; } }}printf("Changed string: %s\n", text);

Example: Ex50.c

Page 21: Computers and programming The 4 th lecture Jiří Šebesta

TOPIC OF THE NEXT LECTURE

1. Functions

THANK YOU FOR YOUR ATTENTION