Introduction to Programming in C 8 תרגול 29. 08. 2011 1 1
דוגמא נוספת #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 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 <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 = '