Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.

Similar presentations


Presentation on theme: "By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW."— Presentation transcript:

1 by Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW

2 by Senem Kumova Metin 2 - POINTERS - int x= 5 ; int * p; /* Pointers are declared in programs and then used to take addresses !!! */ printf(“value of x : %d”, x); printf(“address of x in memory : %d”, &x); p=&x; // a pointer that can hold the address of an integer printf(“value of x : %d”, x); printf(“value of x : %d”, *p); p=&x …. ….. MEMORY x=(*p)=5

3 by Senem Kumova Metin 3 - POINTERS - int x= 5; int *p1; int *p2=&x; p1=&x; printf(“%d%d%d”,x, *p1, *p2); (*p1)++; printf(“%d%d%d”,x, *p1, *p2); scanf(“%d”, p2); // scanf(“%d”,&(*p2)); printf(“%d%d%d”,x, *p1, *p2); x= *p2 = *p1 MEMORY

4 by Senem Kumova Metin 4 POINTERS: EXAMPLE 1-a void increment1(int * x); int increment2(int x); main() { int a=1; increment1(&a); a=increment2(a);} void increment1(int * x) // no need to return { (*x)++;} int increment2(int x) { x++; return x;}

5 by Senem Kumova Metin 5 POINTERS: EXAMPLE 1-b void incrementboth1(int * x, int *y); int incrementboth2(int x, int y); main() { int a=1; int b=2; incrementboth2(&a,&b); a=incrementboth1(a,b); // b=incrementboth1(a,b); } void increment1(int * x, int *y) { (*x)++; (*y)++; } int incrementboth2(int x, int y) { x++; y++; return x; //???? //or return y; }

6 by Senem Kumova Metin 6 POINTERS: EXAMPLE2 void convert_upper(char * x) // no need to return { if( *x =’a’) *x= *x+’a’-’A’;} main() { char a; for (i=0;i<10;i++) {scanf(“%c”,&a); printf(“%c\n ”,a); convert_upper(&a); printf(“%c\n ”,a);} }

7 by Senem Kumova Metin 7 POINTERS: EXAMPLE3 void main() {int a=20; int b=30; swap1 (a, b); swap2 (&a, &b); } void swap1(int x, int y) {int tmp; tmp=x; x=y; y=tmp; } void swap2(int *x, int *y) {int tmp; tmp = *x; // get value pointed by x. *x = *y; // assign value pointed by y to x *y = tmp; }

8 by Senem Kumova Metin 8 ARRAYS A collection of variables with identical properties int ar[100]; Individual members may be selected by an index ar[0]=10; ar[2]=15;

9 by Senem Kumova Metin 9 ARRAYS : INITIALIZATION float f1[]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration without the size*/ float f2[4]= { 1.0, 1.1, 1.2, 1.3 }; /* declaration with size */ char z[] =“abc”; // char arrays char z[] = {‘a’, ‘b’, ‘c’, ‘\0’}

10 by Senem Kumova Metin 10 ARRAY: Example1 main() { int x[10], k; for(k=0; k<10; k++) { x[k]=0; printf(“%d\n”, x[k]); //what will be the output ??? }

11 by Senem Kumova Metin 11 ARRAY: Example2 main() { int x[10], k, sum=0; for(k=0; k<10; k++) scanf(“%d”, &x[k]); for(k=0; k<10; k++) { printf(“%d”, x[k]); sum=sum + x[k]; } printf(“sum is %d”, sum); }

12 by Senem Kumova Metin 12 Relationship between Pointers and Arrays An array name by itself is an adress or a pointer value int a[5]; Address of first  &a[0] == a  Array’s name element First element  a[0] == *a Second element  a[1] == *(a+1) Third element  a[2] == *(a+2). N’th element  a[N] == *(a+N)

13 by Senem Kumova Metin 13 Relationship between Pointers and Arrays : Example main() { int x[10], k; for(k=0; k<10; k++) { x[k]=k; printf(“%d\n”, x[k]); } main() { int x[10], k; for(k=0; k<10; k++) { *(x+k)=k; printf(“%d\n”, *(x+k)); }

14 by Senem Kumova Metin 14 Arrays as Function Arguments : Example 1 // Find the total value of integers in an array main() { int x[9], r; r=sum(x,9); //sum(&x[0],9) } double sum( int a[], int n) // double sum( int * a,int n) { int i; int result =0; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4013623/slides/slide_14.jpg", "name": "by Senem Kumova Metin 14 Arrays as Function Arguments : Example 1 // Find the total value of integers in an array main() { int x[9], r; r=sum(x,9); //sum(&x[0],9) } double sum( int a[], int n) // double sum( int * a,int n) { int i; int result =0; for (i=0; i

15 by Senem Kumova Metin 15 Arrays as Function Arguments : Example 2 // Fill an integer array by using another integer array main() { int x[3]; int y[3]= {1,2,3}; fill(x,y,3); //fill(&x[0],&y[0],3) } void fill( int a[], int b[], int n) { int i; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4013623/slides/slide_15.jpg", "name": "by Senem Kumova Metin 15 Arrays as Function Arguments : Example 2 // Fill an integer array by using another integer array main() { int x[3]; int y[3]= {1,2,3}; fill(x,y,3); //fill(&x[0],&y[0],3) } void fill( int a[], int b[], int n) { int i; for (i=0; i

16 by Senem Kumova Metin 16 Arrays as Function Arguments : Example 3 // Array searching (ITERATION) main() { int x[6]= {1,2,3,4,5,6}; int search_value=9; search(x,6,search_value);} void search( int a[], int size, int number) { int i;int flag=0; for (i=0; i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4013623/slides/slide_16.jpg", "name": "by Senem Kumova Metin 16 Arrays as Function Arguments : Example 3 // Array searching (ITERATION) main() { int x[6]= {1,2,3,4,5,6}; int search_value=9; search(x,6,search_value);} void search( int a[], int size, int number) { int i;int flag=0; for (i=0; i

17 by Senem Kumova Metin 17 Arrays as Function Arguments : Example 4 // Array searching (RECURSION) main() { int x[6]= {1,2,3,4,5,6}; int search_value=9; search(x,6, search_value);} void search(int a[], int size, int number) { if(size==0) printf(“Not found”); else if(a[0]==number)printf(“Found”); else search(a+1,size-1,number); }

18 by Senem Kumova Metin 18 Arrays as Function Arguments : Example 5 // Character Array searching (Iteration) main() { char x[6]= “abcd”; char search_value=‘g’; search(x,search_value);} void search( char a[], char search_value) { int i;int flag=0; for (i=0; a[i]!=0; i++) if(a[i]== search_value) flag=1; if(flag==0) printf(“Not found”); else printf(“Found”); }

19 by Senem Kumova Metin 19 Creating Dynamic Arrays !!! Calloc : contiguous memory allocation Malloc : memory allocation int N=5; int * a =(int *)malloc(N *sizeof(int)); char * x =(char *)malloc(N * sizeof(char)); free(a); size of array size of each element

20 by Senem Kumova Metin 20 Creating Dynamic Arrays !!! Calloc : contiguous memory allocation Calloc initializes each element to zero int N=5; int * a = (int *)calloc(N, sizeof(int)); char * x = (char *)calloc(N, sizeof(char));

21 by Senem Kumova Metin 21 Creating Arrays !!! 1.int a[5]; 2.int * a = (int *)calloc(5, sizeof(int)); 3.int * a = (int *)malloc(5* sizeof(int)); ………… for(i=0;i<5;i++) {scanf(“%d”,&a[i]); printf(“%d”,a[i]); } ………. func(a,5);

22 by Senem Kumova Metin 22 DYNAMIC ARRAY : DYNAMIC ARRAY : Example main() { int * x, k,s; printf(“give array size”); scanf(“%d”,&s); x=malloc(s*sizeof(int)); for(k=0; k { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/12/4013623/slides/slide_22.jpg", "name": "by Senem Kumova Metin 22 DYNAMIC ARRAY : DYNAMIC ARRAY : Example main() { int * x, k,s; printf( give array size ); scanf( %d ,&s); x=malloc(s*sizeof(int)); for(k=0; k

23 by Senem Kumova Metin 23 ARRAY SORTING You will review sorting at home –Bubble Sort –Selection Sort –Merge Sort –etc.

24 by Senem Kumova Metin 24 STRINGS One dimensional arrays of type char String constants are terminated by the end of string sentinel \0 (null) –char st1[] = {‘a’,’b’,’c’,’\0’}; printf(“%s”, st1); –char * st2 = “abc”; printf(“%s”, st2);

25 by Senem Kumova Metin 25 STRING HANDLING FUNCTIONS –char *strcat(char *s1, const char *s2) char d1 [] =“abc”; char d2 [] =“def”; strcat(d1,d2) d1  ???? d2  ???? –int strcmp(const char *s1, const char *s2) char d1 [] =“abc”; char d2 [] =“def”; strcmp(d1,d2) ----> >0 strcmp(d2,d1) ----> <0 strcmp(d1, d1) ----> =0

26 by Senem Kumova Metin 26 STRING HANDLING FUNCTIONS –char *strcpy(char *s1, const char *s2) char d1 [] =“abc”; char d2 [] =“def”; strcpy(d1,d2) d1  ???? d2  ???? –size_t strlen(const char *s) char d1 [] =“abc”; char d2 [] =“def”; strlen(d1)  ??? strlen(strcpy(d1,d2))  ???


Download ppt "By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW."

Similar presentations


Ads by Google