4. chapter iii

16
An Introduction to Pointers

Transcript of 4. chapter iii

Page 1: 4. chapter iii

An Introduction to Pointers

Page 2: 4. chapter iii
Page 3: 4. chapter iii

• A pointer(address variable) is a variable whose value is used to point to another variable.Ex:

long int x, y;y = &x;

(assigns address of the long integer variable x to another variable, y.)

Ex: #include <stdio.h> main() { char c; int x; float y; printf("c: address=0x%p, content=%c\n", &c, c); printf("x: address=0x%p, content=%d\n", &x, x); printf("y: address=0x%p, content=%5.2f\n", &y, y); c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%c\n", &c, c); printf("x: address=0x%p, content=%d\n", &x, x); printf("y: address=0x%p, content=%5.2f\n", &y, y); return 0; }NOTE: %p, %u, %lu

Page 4: 4. chapter iii

• Pointer has Left value and Right value.– Left value: address its self– Right value: which is the content of the pointer, is the address of another variable.– data-type *pointer-name;

• Ex:#include <stdio.h>main(){ char c, *ptr_c; int x, *ptr_x; float y, *ptr_y; c = `A'; x = 7; y = 123.45; printf("c: address=0x%p, content=%c\n", &c, c); printf("x: address=0x%p, content=%d\n", &x, x); printf("y: address=0x%p, content=%5.2f\n", &y, y); ptr_c = &c; printf("ptr_c: address=0x%p, content=0x%p\n", &ptr_c, ptr_c); printf("*ptr_c => %c\n", *ptr_c); ptr_x = &x; printf("ptr_x: address=0x%p, content=0x%p\n", &ptr_x, ptr_x); printf("*ptr_x => %d\n", *ptr_x); ptr_y = &y; printf("ptr_y: address=0x%p, content=0x%p\n", &ptr_y, ptr_y); printf("*ptr_y => %5.2f\n", *ptr_y); return 0;}

Page 5: 4. chapter iii

Storing Similar Data Items• array is a collection of variables that are of the same data type.

– data-type Array-Name[Array-Size];EX:#include <stdio.h>main(){ int i; int list_int[10]; for (i=0; i<10; i++){ list_int[i] = i + 1; printf( "list_int[%d] is initialized with %d.\n", i, list_int[i]); } return 0;}

Page 6: 4. chapter iii

One dimensionl Array

• One dimensional array– Int a[10], float x[50], char str[30];– Int a[10]={12,3,4,65,6,2,3}; int

a[]={1,2,3,4,5,6,7,8};– Char str[30]=“Hello how well?”;– ‘\0’=null remain

Page 7: 4. chapter iii

Ex:#include <stdio.h>main(){ char array_ch[7] = {`H', `e', `l', `l', `o', `!’}; int i; for (i=0; i<7; i++) printf("array_ch[%d] contains: %c\n", i, array_ch[i]); /*--- method I ---*/ printf( "Put all elements together(Method I):\n"); for (i=0; i<7; i++) printf("%c", array_ch[i]); /*--- method II ---*/ printf( "\nPut all elements together(Method II):\n"); printf( "%s\n", array_ch); return 0;}Ex:#include <stdio.h> main() { char array_ch[15] = {`C', ` `, `i', `s', ` `, `p', `o', `w', `e', `r', `f', `u', `l', `!', `\0'}; int i; /* array_ch[i] in logical test */ for (i=0; array_ch[i] != `\0'; i++)printf("%c", array_ch[i]); return 0; }

Page 8: 4. chapter iii

INSERT, SEARCH, SORT, DELETE in one demensional Array

Page 9: 4. chapter iii

Multidimensional Arrays

• data-type Array-Name[Array-Size1][Array-Size2]. . .[Array-SizeN];– int array_int[2][3];– int array_int[2][3] = {{1, 2, 3}, {4, 5, 6}};

Page 10: 4. chapter iii

Unsized Arrays

• int list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90};• char list_ch[][2] = { `7', `A', `b', `B',`c', `C',`d',

`D',`e', `E', `f', `F',`g', `G'};

Page 11: 4. chapter iii

Manipulating Strings

• a string is a character array terminated by a null character (\0).

• char array_ch[7] = {`H', `e', `l', `l', `o', `!', `\0'};• char str[7] = "Hello!";• char str[] = "I like C.";• char *ptr_str = "I teach myself C.";

Page 12: 4. chapter iii

• char ch = `x';• char str[] = "x";• char *ptr_str;

ptr_str = "A character string.";

Page 13: 4. chapter iii

#include <stdio.h> main() { char str1[] = {`A', ` `, `s', `t', `r', `i', `n', `g', ` `, `c', `o', `n', `s', `t', `a', `n', `t', `\0'}; char str2[] = "Another string constant"; char *ptr_str; int i; /* print out str2 */ for (i=0; str1[i]; i++) printf("%c", str1[i]); printf("\n"); /* print out str2 */ for (i=0; str2[i]; i++) printf("%c", str2[i]); printf("\n"); /* assign a string to a pointer */ ptr_str = "Assign a string to a pointer."; for (i=0; *ptr_str; i++) printf("%c", *ptr_str++); return 0; }

Page 14: 4. chapter iii

#include <stdio.h> main() { char str[80]; int i, delt = `a' - `A'; printf("Enter a string less than 80 characters:\n"); gets( str ); i = 0; while (str[i]){ if ((str[i] >= `a') && (str[i] <= `z')) str[i] -= delt; /* convert to upper case */ ++i; } printf("The entered string is (in uppercase):\n"); puts( str ); return 0; }

Page 15: 4. chapter iii

char *str = "Hello World!";

• Alternatively one might think that a pointer to string in C is a pointer to a char array

/ a pointer to a pointer to a char. • char *str = "Hello World!"; – str is a pointer to a string.– str is a pointer to a char array.– But str is a pointer to a char (not a pointer to a pointer to a

char). str is of type char * .(and here str points to H).– Ended by \0 null

– char *v1=“A”; 2 bytes– char v1=‘A’; 1 byte– ‘A’ + 20 + ‘C’ but not “A” + 20 + “C”

H e l l o W o r l ! \0

A \0

A

Page 16: 4. chapter iii

Arr[6]=“12345”;

• char arr[6]=“gfhgf”;• char arr[6]={‘1’,’2’,’3’,’4’,’5’};• char arr[]=“3212432432”;

1 2 3 4 5 \0