Thread: Problem from book The C Programming Language by Ritchie

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    3

    Problem from book The C Programming Language by Ritchie

    Hello everyone,

    I am new to C and am currently working through the book "The C Programming Language" by Kernighan and Ritchie.

    The following code example, found on page 30, gets lines of inputs from the user, determines which line is the longest, and then prints the longest line.

    I have tested this code and it seems to work as intended.

    My question is regarding the function "copy" which receives two arrays of characters and copies one array to the other. I am wondering how this new array ends up being stored in the array "longest" found in the main function. There is no return type of the function, so it is not returned and it is my understanding that arguments in C are value not reference. So, what is happening here? Any explanation would be appreciated. Thanks!

    Code:
    #include <stdio.h>
    
    
    #define MAXLINE 1000
    
    
    int get_line(char line[], int maxline);
    void copy(char to[], char from []);
    
    
    main(){
        
        int len;
        int max;
        char line[MAXLINE];
        char longest[MAXLINE];
        
        max = 0;
        
        while((len = get_line(line, MAXLINE)) > 0)
            if(len > max) {
                max = len;
                copy(longest, line);
            }
        if(max > 0)
            printf("%s", longest);
        
        return 0;
    }
    
    
    /*getline: read a line into s, return lenght*/
    
    
    int get_line(char s[], int lim){
        
        int c, i;
        
        for(i=0; i < lim - 1 && (c=getchar()) != EOF && c != '\n'; ++i)
            s[i] = c;
        
        if(c == '\n'){
            s[i] = c;
            ++i;
        }
        
        s[i] = '\0';
        
        return i;
    }
    
    
    /*copy: copy 'from' into 'to'; assume to is big enough */
    
    
    void copy(char to[], char from[]){
        
        int i;
        
        i = 0;
        
        while((to[i] = from[i]) != '\0')
            ++i;
    }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Well, it would be more correct to regard to and from in the copy function as pointers to the first elements of longest and line, respectively. Once an array has to leave the scope it was declared in, it has to decay to a pointer type in this fashion. So just look at copy() again, but think of it in terms of pointers this time.

    There are notable exceptions, such as, if the parameter takes the address of an array (e.g. &line), and using sizeof() doesn't cause the array to decay, either. Happily, these exceptions will pretty much not matter if you are passing an array to a function. Normally, using & with an array variable is simply a mistake.

    The reason C has this little wrinkle is because copying an array like a normal parameter is more expensive than doing this... C wasn't designed to do it differently, and at the time of C's heyday, it was a smart design move.

    HTH.

  3. #3
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Ok. Thanks for the response. This will take a minute for me to wrap my head around. Not what I expected.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    You should explicitly indicate that main() returns int, and provide the necessary parameters (void if no arguments are used):

    Code:
    int main(void)
    {
        // ...
    }
    This is good practice in general, and mandatory since C99.
    K&R and is great source, but outdated in certain areas.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Take your time, of course. I'd rather give a really complete response than be terse with it and cause more confusion.

    If it helps you to understand, remember that to[i] is syntactic sugar for *(to + i), and that is how you ought to think of it. It all comes down to pointer arithmetic. You can change the array in an indirect way because you are actually using pointers.

  6. #6
    Registered User
    Join Date
    Dec 2016
    Posts
    3
    Quote Originally Posted by whiteflags View Post
    Take your time, of course. I'd rather give a really complete response than be terse with it and cause more confusion.

    If it helps you to understand, remember that to[i] is syntactic sugar for *(to + i), and that is how you ought to think of it. It all comes down to pointer arithmetic. You can change the array in an indirect way because you are actually using pointers.
    Thanks. That makes it much clearer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The C Programming language (K&R) Book
    By kidharris55 in forum General Discussions
    Replies: 14
    Last Post: 09-11-2014, 04:45 AM
  2. The C Programming language (K&R) Book
    By kidharris55 in forum C Programming
    Replies: 3
    Last Post: 09-08-2014, 07:37 PM
  3. binsearch function c programming language book
    By blob84 in forum C Programming
    Replies: 4
    Last Post: 05-17-2011, 04:36 AM
  4. a mistake in the c programming language book.wtf?
    By KidMan in forum C Programming
    Replies: 9
    Last Post: 01-17-2006, 04:33 AM
  5. Newbie question: Kernighan-Ritchie C book- problem
    By plutonas in forum C Programming
    Replies: 2
    Last Post: 06-29-2005, 12:50 PM

Tags for this Thread