example from c programming "a modern approach"

This is a discussion on example from c programming "a modern approach" within the C Programming forums, part of the General Programming Boards category; Hello everybody, I have been reading C programming "a modern approach" and I must say this has been my best ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    35

    example from c programming "a modern approach"

    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
    Code:
    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
    my doubts/questions

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

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    i is the number of letters read, which it is true that you are ignoring.

    As to the other, that is the simple explanation. If you have a list of things, and you want to put something in the middle, you have to move half the list down one spot to put the new item in the empty spot.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by convenientstore View Post
    1) line 58, not sure why i is being returned at all. .Looks like useless return?
    Look at how i is being generated and subsequently used in the for() and while loops. When a for loop breaks, it ceases to iterate, leaving "i" at a value less than it would have been. So if no match is found with the strcmp in the loop at line 30, "i" will end up equal to num_remind. If you look at the conditions of the loop on line 32, you will see that if i == num_remind, this loop will not even happen once.

    Just apply the same logic to the while loop in read_line(). It's just dead simple arithmetic, on the level of 2+2=4.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thanks guys, I am gonna digest this bit and try few more things and get back to you guys..

    thank you once again!!

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    thanks guys,

    I certainly now understand what those loops are doing.
    one more thing however, I really don't understand why it was necessary for multi dimensional array to be need for

    11 char reminders[MAX_REMIND][MSG_LEN+3];

    looks like max_remind is really not being used.. or is this because i don't understand the multi array well ?
    I think i got the grasp of how multi array is really single dimensional array... but i dont get this piece of code

    how does
    reminders[1] = 'birtday" different from
    reminders[][1] = 'birthday' ?? or is this not even what the multi array in program is trying to describe?

    or better yet

    11 char reminders[MAX_REMIND][MSG_LEN+3];

    instead of above

    char reminders[MSG_LEN+3];

    would it have worked?
    Last edited by convenientstore; 06-13-2009 at 11:16 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    char reminders[MSG_LEN+3] is one string. If you want, say, more than one string, then you need, say, an array of strings such as char reminders[MAX_REMIND][MSG_LEN+3].

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    I would definitely study multiple dimensional array implementations, as that would be more efficient for what you need and shorten your coding a bit.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    can someone provide me w/ great tutorial or link that explains the relationship between multi dimensional array and pointer?

    I can see that char reminders[MSG_LEN+3] is one string vs char reminders[MAX_REMIND+3][MSG_LEN+3] can contain multiple strings...

    But what it really means is what I don't understand.
    I have been reading that multi dimension doesn't really exist and everything is just linear in memory.

    so
    let's say char reminders[10][100] then, that means I can add 1,000 items in there..
    so first item would be
    reminders[0][0] ------> this would be very first item
    second item would be
    reminders[0][1] --------> this would be second item

    and that's why

    strcpy( reminders[i], day_str );


    works....

    am i understanding this correctly?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Multiple dimension arrays exist as single arrays for each dimension. Take a 2D array as an example:

    char A[row][col]

    Each row is the base of another 1D char array. That's why the first part of A[row], when you malloc it, is just an array of pointers. Only the columns need to be malloc'd with the size of the data type, because only the columns hold the data.

    In memory, you'd see it as:
    char A[3][3]=[a][b][c][d][e][f][g][h][i]

    We can correctly visualize that as:
    A[0]=[a][b][c]
    A[1]=[d][e][f]
    A[2]=[g][h][i]

    With no harm done - the computer is not confused. Yet, one char's worth of memory after c, you would find d.

    Pointers are so powerful and useful, that they are a big part of every OS, and every programming language worth a hoot.

    In many languages, they are simply put "behind the curtain", out of sight. (Think of the Wizard of Oz). Not in C, however.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Modern DEBUG?
    By Uwar in forum Tech Board
    Replies: 10
    Last Post: 06-02-2009, 02:41 AM
  2. modern mathplot library in c
    By stabu in forum C Programming
    Replies: 4
    Last Post: 05-26-2009, 04:11 PM
  3. a qestion about modern editors
    By Masterx in forum C++ Programming
    Replies: 5
    Last Post: 02-01-2009, 12:04 AM
  4. Invoking system commands... "A better way?"
    By The_Muffin_Man in forum C++ Programming
    Replies: 10
    Last Post: 06-10-2004, 04:57 AM
  5. Modern Algebra
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-10-2003, 09:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21