Introduction to Programming in C 8 29 08

  • Slides: 23
Download presentation
Introduction to Programming in C 8 תרגול 29. 08. 2011 1 1

Introduction to Programming in C 8 תרגול 29. 08. 2011 1 1

 דוגמא נוספת #include <stdio. h> void main(){ int x , *px; int y,

דוגמא נוספת #include <stdio. h> void main(){ int x , *px; int y, *py ; /*Define a variable of type int named "x" and a variable of type pointer to int named "px". */ px=&x; /*Assign the address of "x" to be the value of "px". */ py=&y; scanf("%d%d", px, py); /*Read two integers values from the input and assign them to "x" and "y". Make sure that you understand why!!! */ printf("x=%d , y=%dn", *px, *py); /*Print the values of the x and y which are the variables pointed to by px and py. */

2 תרגיל : תוכנית לחישוב אורך המחרוזת עם שימוש במצביעים #include <stdio. h> #define

2 תרגיל : תוכנית לחישוב אורך המחרוזת עם שימוש במצביעים #include <stdio. h> #define MAX_LENGTH 80 int my_strlen (char *s){ char *p = s; while (*p) p++; return p - s; } void main(){ char str[MAX_LENGTH]; int len; printf("Enter a string: "); gets(str); len = my_strlen(str); printf("The lenght of the string %s is %d n", str, len); }

3 תרגיל : מחרוזות הממחישה שימוש במצביעים 2 תוכנית לחיבור #include <stdio. h> #include

3 תרגיל : מחרוזות הממחישה שימוש במצביעים 2 תוכנית לחיבור #include <stdio. h> #include <string. h> #define MAX_LENGTH 81 /*Option #1*/ void string. Cat(char *s 1, char *s 2){ while (*s 1 ) ++s 1; /*We could equivalently have written: s 1++; */ do { *s 1 = *s 2; s 1++; s 2++; } while(*s 2); *s 1 = ''; } /*Option #2*/ void string. Cat(char *s 1, char *s 2){ while (*s 1 ) ++s 1; while(*(s 1++) = *(s 2++)); }

/*Option #3*/ 3 המשך תרגיל void string. Cat(char *s 1, char *s 2){ strcpy(s

/*Option #3*/ 3 המשך תרגיל void string. Cat(char *s 1, char *s 2){ strcpy(s 1+strlen(s 1), s 2); } void main(){ char str 1[MAX_LENGTH], str 2[MAX_LENGTH]; printf("Enter a string #1 with maximum %d characters: ", (MAX_LENGTH-1)/2); gets(str 1); printf("Enter a string #2 with maximum %d characters: ", (MAX_LENGTH-1)/2); gets(str 2); string. Cat(str 1, str 2); /*We can use any (but not more than one) of the options above! */ printf("The final string is "%s" n", str 1); }

4 המשך תרגיל void main(){ char st 1[]="bnbnbacdhghg", st 2[]="acd"; char *c; if(c= strstr(st

4 המשך תרגיל void main(){ char st 1[]="bnbnbacdhghg", st 2[]="acd"; char *c; if(c= strstr(st 1, st 2)) printf ("The first character in both st 2 and st 1 is %cn ", *c); else printf ("Sorry, st 2 isn't contained in st 1!!!n"); }

 אופרטורים float f; sizeof האופרטור . מחזיר את גודלו של הטיפוס בבתים sizeof

אופרטורים float f; sizeof האופרטור . מחזיר את גודלו של הטיפוס בבתים sizeof האופרטור sizeof (name_of_charecter): תבנית : אילו הינו כותבים. 4 מחזיר sizeof (float) : לדוגמא . (4) היה מחזיר את אותו דבר sizeof(f) : malloc שימוש בפונקציה variable_pointer = (pointer_type) malloc (size_of_memory); : דוגמא int size, *p_list; printf("Enter the number of elements: "); scanf("%d", &size); p_list = (int*)malloc (size * sizeof(int));

5 המשך תרגיל for (i=0; i<rows; i++){ /*Fill in the different rows: */ if

5 המשך תרגיל for (i=0; i<rows; i++){ /*Fill in the different rows: */ if (!(array[i]=(int *)malloc(cols*sizeof(int)))){ for (t=0; t<i; t++) /*Free all priory allocated memory: */ free(array[t]); free(array); printf("Memory allocation failed, quiting… "); return; /*Terminate the program!*/ } for (j=0; j<cols; j++) /*Fill in the different columns: */ array[i][j]=i*j; } for (i=0; i<rows; i++){ /*Print the different rows: */ for (j=0; j<cols; j++) printf("%d ", array[i][j]); printf("n"); }

5 המשך תרגיל /*Free allocated memory: */ for(i=0; i<rows; i++) free(array[i]); free(array); }

5 המשך תרגיל /*Free allocated memory: */ for(i=0; i<rows; i++) free(array[i]); free(array); }

Bye!!!

Bye!!!