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

By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW

Embed Size (px)

Citation preview

by Senem Kumova Metin 1

POINTERS + ARRAYSPOINTERS + ARRAYS

+ STRINGS+ STRINGS

REVIEWREVIEW

by Senem Kumova Metin 2

- POINTERS -- 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

by Senem Kumova Metin 3

- POINTERS -- 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

by Senem Kumova Metin 4

POINTERS: EXAMPLE 1-aPOINTERS: 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; }

by Senem Kumova Metin 5

POINTERS: EXAMPLE 1-bPOINTERS: EXAMPLE 1-bvoid 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;

}

by Senem Kumova Metin 6

POINTERS: EXAMPLE2POINTERS: EXAMPLE2void convert_upper(char * x) // no need to return { if( *x <= ‘z’ && *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); }

}

by Senem Kumova Metin 7

POINTERS: EXAMPLE3POINTERS: EXAMPLE3void 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; }

by Senem Kumova Metin 8

ARRAYSARRAYS• A collection of variables with identical

properties

int ar[100];

• Individual members may be selected by an index

ar[0]=10;ar[2]=15;

by Senem Kumova Metin 9

ARRAYS : INITIALIZATIONARRAYS : 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’}

by Senem Kumova Metin 10

ARRAY: Example1ARRAY: Example1main(){

int x[10], k;

for(k=0; k<10; k++){

x[k]=0;printf(“%d\n”, x[k]); //what will be the output ???

}}

by Senem Kumova Metin 11

ARRAY: Example2ARRAY: Example2main(){

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

}

by Senem Kumova Metin 12

Relationship between Pointers and Relationship between Pointers and ArraysArrays

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

by Senem Kumova Metin 13

Relationship between Pointers and Relationship between Pointers and Arrays : ExampleArrays : 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)); }

}

by Senem Kumova Metin 14

Arrays as Function Arguments :Arrays as Function Arguments :Example 1Example 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<n; i++)result+=a[i]; // result+=*(a+i)

return result; }

by Senem Kumova Metin 15

Arrays as Function Arguments : Arrays as Function Arguments : Example 2Example 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<n; i++)a[i] =b[i]; // *(a+i)=*(b+i)

}

by Senem Kumova Metin 16

Arrays as Function Arguments : Arrays as Function Arguments : Example 3Example 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<size; i++)if(a[i]==number) flag=1;

if(flag==0) printf(“Not found”);else printf(“Found”);

}

by Senem Kumova Metin 17

Arrays as Function Arguments : Arrays as Function Arguments : Example 4Example 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);

}

by Senem Kumova Metin 18

Arrays as Function Arguments : Arrays as Function Arguments : Example 5Example 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”);

}

by Senem Kumova Metin 19

Creating Dynamic Arrays !!!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

by Senem Kumova Metin 20

Creating Dynamic Arrays !!!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));

by Senem Kumova Metin 21

Creating Arrays !!!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);

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<s; k++) scanf(“%d”, &x[k]);

for(k=0; k<s; k++) printf(“%d ”,x[k]);

}

by Senem Kumova Metin 23

ARRAY SORTINGARRAY SORTING• You will review sorting at home

– Bubble Sort– Selection Sort– Merge Sort – etc.

by Senem Kumova Metin 24

STRINGS 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);

by Senem Kumova Metin 25

STRING HANDLING FUNCTIONSSTRING 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) ----> <0strcmp(d1, d1) ----> =0

by Senem Kumova Metin 26

STRING HANDLING STRING HANDLING FUNCTIONSFUNCTIONS

– 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)) ???