I don't understand K&R example

This is a discussion on I don't understand K&R example within the C Programming forums, part of the General Programming Boards category; Hi, all. I'm writing because there's something I don't understand in K&R's book. In Section 1.9 (character arrays), an example ...

  1. #1
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    I don't understand K&R example

    Hi, all. I'm writing because there's something I don't understand in K&R's book. In Section 1.9 (character arrays), an example code is given in which the main function calls a function named getline. When getline is called for the first time, two arguments are (supposedly) passed to it, a character array named line and an integer named maxline.
    Right at this point I get confused. Up to this point in the code, the line array has only been declared but it has not been assigned any value. So, if line has not been assigned any value before calling getline, how does any character array get passed to this last function?
    In Section 1.7 (functions), a function named power is called by main. But this time, two previously defined arguments are passed to power. This example I can understand, but the inner working of the example in Section 1.9 is uncomprehensible for me at my current level in C programming.
    So, if anyone knows how to give a clear explanation about this subject, I'd love to hear it.

    Thanks in advance to those who take the time to read this post.

    Roberto.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    I'm not familiar with the section you're talking about, but I'm assuming the getline() function fills the array. Something like:
    Code:
    void getline(char *buffer, int max)
    {
      strncpy(buffer, "This is a zucchini", max);
      buffer[max] = '\0';
    }
    
    int main(void)
    {
      char buffer[512];
    
      getline(buffer, sizeof(buffer));
      return 0;
    }
    Am I way off?
    If you understand what you're doing, you're not learning anything.

  3. #3
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    I'm talking about the first chapter

    Hi. I'm talking about something that's in chapter 1, section 9. I have a spanish copy of the book. In my copy, the code I'm talking about is in page 31.

    Hope this helps.

    I didn't quite understand the code you wrote.

    Thanks,

    Roberto.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    why dont YOU show US the code.

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by rllovera
    Hi, all. I'm writing because there's something I don't understand in K&R's book. In Section 1.9 (character arrays), an example code is given in which the main function calls a function named getline. When getline is called for the first time, two arguments are (supposedly) passed to it, a character array named line and an integer named maxline.
    Right at this point I get confused. Up to this point in the code, the line array has only been declared but it has not been assigned any value. So, if line has not been assigned any value before calling getline, how does any character array get passed to this last function?
    In Section 1.7 (functions), a function named power is called by main. But this time, two previously defined arguments are passed to power. This example I can understand, but the inner working of the example in Section 1.9 is uncomprehensible for me at my current level in C programming.
    So, if anyone knows how to give a clear explanation about this subject, I'd love to hear it.

    Thanks in advance to those who take the time to read this post.

    Roberto.
    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    int main(void)
    {
        int len;
        int max;
        char line[MAXLINE];         /* declare array of type char with
                                       MAXLINE (1000) elements */
        char longest[MAXLINE];
        max = 0;
    
        /* call getline function and pass the address 
           of the first element of the array (line), and the 
           size of the array (MAXLINE, or 1000) */
        while ((len = getline(line, MAXLINE)) > 0)
           .....
    What happens is that the function getline() is called, which reads a line into the array that is passed to it. After getline is called, and assuming the user entered some text, the array line has the text stored in it. Then when the last function copy is called, it is passed the array line and another array longest so it can copy the contents of one array into another. If you want to get a little more technical, when getline is called (and keep this in mind for any function which has an array as a parameter) it is actually passed the address of line[0]. In this way, the function can work directly on the array line. In effect, the call to getline is telling the function, 'here is an address of the first element of an array (in this function it is line, which is like saying line[0], for the array name is essentially the same thing as saying array[0]), so that you know where to find the variable in order to work on it (eg., store characters in it), and also here is the size of that array so you don't overstep the bounadaries of that array' (because overstepping the boundaries of an array is a bad thing).

    ~/
    Last edited by kermit; 10-21-2004 at 08:49 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Ahh, so I wasn't way off at all =)
    If you understand what you're doing, you're not learning anything.

  7. #7
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    Im beginning to understand

    Hi. First of all, Id like to thank all of you who replied to my message for your time and effort.

    That said, I go back to business. I assume (from what kermit posted) that all that the function main must pass to the function getline is the address of the first element of the array line and its size in order for the latter function to work properly. There is no need for the array to have a value before making the call to getline . Is that the right reasoning? If what I think is right, then it is optional (dependent on what you want the program to perform) to pass a value of a character array to a function when it is called. Please correct me if Im wrong.
    As I said in my first post to this thread, this is the very fact that led me to confusion.

    Since Im new to the world of C programming, this behavior of the language took me by surprise.

    Thanks again to you all,

    Roberto.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    It's the same thing as the strcpy() function:
    Code:
    #include <string.h>
    
    int main(void)
    {
      char str[50];
    
      strcpy(str, "Hello, world!");
      return 0;
    }
    See? str's contents are not defined when you pass str to strcpy(). That's because strcpy() fills the array! If the function is meant to accept a string and then manipulate the data in some way then you'd need to fill str with something meaningful before you used it. For example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char str[50];
    
      puts(str);
      return 0;
    }
    puts() isn't designed to accept an undefined string so you'll just get garbage output. So there's no universal "You don't have to fill the character array before passing it to a function" rule. It depends on what the function expects.
    If you understand what you're doing, you're not learning anything.

  9. #9
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    thanks

    Thanks, itsme86. Your last post to this thread clarified the point I had a doubt about.
    That's all. Just wanted to thank you.

    Roberto.

Popular pages Recent additions subscribe to a feed

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