Thread: K&R example does not compile under Ubuntu 10.10 using CC. Example from Sect 1.9

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    K&R example does not compile under Ubuntu 10.10 using CC. Example from Sect 1.9

    From page 29 K&R 2nd 1988, So here it goes (With some mods from me):

    insert
    Code:
    #include <stdio.h>
    
    main() {
    
        int len;
        int max =0;
        char line[500];
        char longest[500];
    
        while( (len = getline(line, 500)) > 0) {
            if( len > max ){
                max = len;
                copy(longest, line);
            }
        }
        if (max > 0)
            printf("%s", longest);
    //  return 0;
    }
    
    int getline(char that_line[], int lim){
        int c, i;
    
        for (i=0; i < lim-1 && (c=getchar()) != EOF && c!='\n'; ++i)
            that_line[i] = c;
    
        if (c == '\n') {
            that_line[i] = c;
            ++i;
        }
        that_line[i] = '\0';
    
        return i;
    }
    
    void copy(char dest[], char from[]) {
        int i = 0;
    
        while( (dest[i] = from[i]) != '\0')
            ++i;
    }
    $cc example.c

    Here the the error msgs:
    19.c: In function ‘main’:
    19.c:10: warning: passing argument 1 of ‘getline’ from incompatible pointer type
    /usr/include/stdio.h:671: note: expected ‘char ** __restrict__’ but argument is of type ‘char *’
    19.c:10: warning: passing argument 2 of ‘getline’ makes pointer from integer without a cast
    /usr/include/stdio.h:671: note: expected ‘size_t * __restrict__’ but argument is of type ‘int’
    19.c:10: error: too few arguments to function ‘getline’
    19.c: At top level:
    19.c:21: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
    19.c:36: warning: conflicting types for ‘copy’
    19.c:13: note: previous implicit declaration of ‘copy’ was here

    So whats wrong with the code..
    Has the standard changed much since to C99 or something that the compiler uses..

    Yeah the code was modded bit by me...
    but I doubt if I wrote it carbon copy- or use Visual Studio the result will differ ?

    Like many ppl today, I guess I m from Java..

    Can someone explain why it failed & what has changed maybe since..
    What's a good reference book for learning the language.
    Assume I know quite some about Java can program well already..

    It's just my prob with C with playing pointers etc too

    Thanks a lot.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What K&R edition do you have? You need to declare the functions before using them. Nothing special, just the function's return type, name and argument list.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    First, you should have some prototypes before main (like the example in THE BOOK does)

    int getline(char that_line[], int lim);
    void copy(char dest[], char from[]);

    Second, you'll probably need to compile with
    gcc -ansi example.c
    to hide the alternate declarations of getline and copy (note: previous implicit declaration of ‘foo’ was here)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Yes your method worked. Thanks.

    I use the 1988 2nd ed book.

    =================
    But, in order for only $ gcc example.c to work..
    How should I change to the code then ?

    And maybe what current C book / online reference is best to use with gcc standard ?

    Thanks again.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Oops..
    19.c is the file name

    /home/me/cwork >gcc 19.c
    19.c:3: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here
    19.c:24: error: conflicting types for ‘getline’
    /usr/include/stdio.h:671: note: previous declaration of ‘getline’ was here

    I just changed the method name into get_a_line( ... )
    it fixed it... using just $ gcc 19.c

    So ok. getline was a method already defined in stdio.h

    But, thanks!!!! hehe

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The stuff in K&R-2Ed should work just fine if you compile with gcc -ansi

    > But, in order for only $ gcc example.c to work..
    > How should I change to the code then ?
    Rename copy and getline to be something more unique - like my_copy and my_getline
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    Yes. I realized that soon after replying.
    So sorry and thanks.

    But how to correctly terminate the program so it prints out the result ?

    /home/me/cwork >./a.out
    test
    sliz
    aaaaaaaaaaaaa

    0
    ^C

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You typically press ctrl-d when at the start of a line to signal EOF to the stdin stream.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ubuntu
    By GReaper in forum Tech Board
    Replies: 8
    Last Post: 09-06-2010, 10:46 PM
  2. Ubuntu 10.04 LTS partitioning??
    By cpjust in forum Tech Board
    Replies: 11
    Last Post: 06-14-2010, 11:35 AM
  3. anyone familiar with ubuntu?
    By ktran03 in forum C Programming
    Replies: 2
    Last Post: 03-01-2009, 06:23 PM
  4. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  5. pos - single sect os
    By ggs in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 12-31-2001, 02:15 PM