Hello everybody,
I have been reading C programming "a modern approach" and I must say this has been my best c book(I am a very very newbie)..
Anyway, below example from that book has me bit lost
Code:1 #include <stdio.h> 2 #include <string.h> 3 4 #define MAX_REMIND 50 5 #define MSG_LEN 60 6 7 int read_line(char str[], int n); 8 9 main() 10 { 11 char reminders[MAX_REMIND][MSG_LEN+3]; 12 char day_str[3], msg_str[MSG_LEN+1]; 13 int day, i, j, num_remind = 0; 14 15 for (;;) { 16 if ( num_remind == MAX_REMIND ) { 17 printf("--No space left--\n"); 18 break; 19 } 20 21 printf("Enter day and reminder: "); 22 scanf("%2d", &day); 23 24 if (day == 0 ) 25 break; 26 sprintf(day_str, "%2d", day); 27 read_line(msg_str, MSG_LEN); 28 29 for ( i = 0; i < num_remind; i++ ) 30 if ( strcmp( day_str, reminders[i]) < 0 ) 31 break; 32 for ( j = num_remind; j > i; j-- ) 33 strcpy( reminders[j], reminders[j - 1] ); 34 35 strcpy( reminders[i], day_str ); 36 strcat( reminders[i], msg_str ); 37 38 num_remind++; 39 } 40 41 printf("\nDay Reminder\n"); 42 for ( i = 0; i < num_remind; i++ ) 43 printf(" %s\n", reminders[i]); 44 45 return 0; 46 47 } 48 49 int read_line(char str[], int n ) 50 { 51 char ch; 52 int i = 0; 53 54 while ( ( ch = getchar() ) != '\n' ) 55 if ( i < n ) 56 str[i++] = ch; 57 str[i] = '\0'; 58 return i; 59 }
for example, I would run below
my doubts/questionsCode:Enter day and reminder: 13 birthday Enter day and reminder: 7 payday Enter day and reminder: 7 zoo day Enter day and reminder: 29 holiday Enter day and reminder: 0 Day Reminder 7 zoo day 7 payday 13 birthday 29 holiday
1) line 58, not sure why i is being returned at all. .Looks like useless return?
2) I am totally lost on lines 29 to 33..
Book describes "It will search the array to determine where the day belongs, using
strcmp to do comparisons" ?? It will then use strcpy to move all strings below
that point down one position"
These lines are very hard for me to understand. Can someone please explain to me in
more dumb down version?
I would really appreciate it...
thank you so much!!



LinkBack URL
About LinkBacks



