Here's the code I am studying from "modern day c programming"
Code:
#include <string.h>
#define MAX_REMIND 50
#define MSG_LEN 60
int read_line(char str[], int n);
main()
{
char reminders[MAX_REMIND][MSG_LEN+3];
char day_str[3], msg_str[MSG_LEN+1];
int day, i, j, num_remind = 0;
for (;;) {
if ( num_remind == MAX_REMIND) {
printf("-- No space left --\n");
break;
}
printf("Enter day and reminder: ");
scanf("%2d", &day);
if ( day == 0 )
break;
sprintf(day_str, "%2d", day);
read_line(msg_str, MSG_LEN);
for (i = 0; i < num_remind; i++ )
if ( strcmp(day_str, reminders[i]) < 0 )
break;
for (j = num_remind; j > i; j-- )
strcpy(reminders[j], reminders[j-1]);
strcpy(reminders[i], day_str);
strcat(reminders[i], msg_str);
num_remind++;
}
printf("\nDay Reminders\n");
for ( i = 0; i < num_remind; i++ )
printf(" %s\n", reminders[i]);
return 0;
}
int read_line(char str[], int n )
{
char ch;
int i = 0;
while ( (ch = getchar() ) != '\n' )
if ( i < n )
str[i++] = ch;
str[i] = '\0';
return i;
}
~
And here's gdb session I am doing:
Two question,
1)why can't I print out value of MAX_REMIND
2)why can't I print out the value of msg_str
Code:
GNU gdb Red Hat Linux (6.3.0.0-1.162.el4rh)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) b main
Breakpoint 1 at 0x80484bf: file big_to_small.c, line 13.
(gdb) r
Starting program: /root/pro/c/big_to_small
Breakpoint 1, main () at big_to_small.c:13
13 int day, i, j, num_remind = 0;
(gdb) s
16 if ( num_remind == MAX_REMIND) {
(gdb) disp num_remind
1: num_remind = 0
(gdb) disp MAX_REMIND
No symbol "MAX_REMIND" in current context.
(gdb) s
21 printf("Enter day and reminder: ");
1: num_remind = 0
(gdb)
23 scanf("%2d", &day);
1: num_remind = 0
(gdb)
Enter day and reminder: 1^H1^H^H^H
24 if ( day == 0 )
1: num_remind = 0
(gdb) k
Kill the program being debugged? (y or n) y
(gdb) b main
Note: breakpoint 1 also set at pc 0x80484bf.
Breakpoint 2 at 0x80484bf: file big_to_small.c, line 13.
(gdb) r
Starting program: /root/pro/c/big_to_small
Breakpoint 1, main () at big_to_small.c:13
13 int day, i, j, num_remind = 0;
1: num_remind = 0
(gdb) s
16 if ( num_remind == MAX_REMIND) {
1: num_remind = 0
(gdb)
21 printf("Enter day and reminder: ");
1: num_remind = 0
(gdb)
23 scanf("%2d", &day);
1: num_remind = 0
(gdb)
Enter day and reminder: 16 birthday
24 if ( day == 0 )
1: num_remind = 0
(gdb) s
26 sprintf(day_str, "%2d", day);
1: num_remind = 0
(gdb)
27 read_line(msg_str, MSG_LEN);
1: num_remind = 0
(gdb) disp msg_str
2: msg_str = '\0' <repeats 60 times>
(gdb) disp MSG_LEN
No symbol "MSG_LEN" in current context.
(gdb) print msg_str
$1 = '\0' <repeats 60 times>
(gdb)