Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computers and programming The 4 th lecture Jiří Šebesta."— Presentation transcript:

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

2 TOPIC 1.Pointers 2.Arrays and pointers 3.Array of pointers 4.Examples

3 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)

4 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.

5 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 = 2 10 B = 1024 B 1 MB = 2 20 B = 1024 kB = 1048576 B 1 GB = 2 30 B = 1024 MB, 1 TB = 2 40 B = 1024 GB 1 PB = 2 50 B = 1024 TB, 1 HB = 2 60 B = 1024 HB, etc.

6 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 integer int 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 b printf("%d\n", y[3]); Example: Ex40.c

7 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 integer int arr[50], i; adr = &(arr[49]);//pointer is set to the last element for(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

8 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 integer int arr[50], i; adr = arr;//pointer is set to the first element for(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

9 Pointers (7/10) Ex. Using relation operators for pointers int *a, *b, *c; // pointers to integer int 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) //address a must be between b and c *a=0; a++; } for(i=0; i<20; i++) printf("%3d", arr[i]); Example: Ex43.c

10 Pointers (8/10) Ex. Integer v in memory in bytes int *a; //pointer to int char *b; //pointer to char int 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

11 Pointers (9/10) Ex. Float in memory in bytes unsigned char *a; //pointer to uchar float num = 1.2; //float value in memory int 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

12 Pointers (10/10) 1,2 in float resolution has form hex: 0x 3 F 9 9 9 9 9 A binary: 0b0-01111111-00110011001100110011010 sign: 0 = + exponent: 0b01111111 = 127 mantissa: 0b00110011001100110011010 = 1677722

13 Arrays and pointers (1/3) int *a; //pointer to int int 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

14 Arrays and pointers (2/3) Multi-dimensional array in memory int *a; //pointer to int int 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

15 Arrays and pointers (3/3) Two-dimensional array in memory: Equivalent entry: arr[i][j][k] = *(arr+i*3*3+j*3+k)

16 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 pointers int 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 }

17 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

18 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

19 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;

20 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

21 TOPIC OF THE NEXT LECTURE 1.Functions THANK YOU FOR YOUR ATTENTION


Download ppt "Computers and programming The 4 th lecture Jiří Šebesta."

Similar presentations


Ads by Google