Thread: Example Code doesn't work!

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    Example Code doesn't work!

    Hi,

    I'm working through Kernighan and Ritchie's The C Programming Language and have copied example code (see below) into Code Blocks to improve my understanding of how it works. Unfortunately I'm getting the error: 'conflicting types for 'getline'...

    Code:
    #include <stdio.h>
    # define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    int main() /* print longest input line */
    {
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];
    
    max = 0;
    while ((len = getline(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 length */
    int getline(char s[], int lim)
    {
    int c, i;
    
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != 10; ++i)
        s[i] = c;
    if (c == 10) {
        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;}
    Any ideas why this is happening?

  2. #2
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Code:
    int getline(char line[], int maxline);
    ...
    int getline(char s[], int lim)
    If you're going to include the argument names in the function prototype, you'd better match the names, otherwise you can have a function prototype like:

    Code:
    int getline(char[], int);

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    I get no compiler warnings or error on this code.
    You might want to check this line of code:
    Code:
    while ((to[i] - from[i]) != '\0') ++i;
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Thanks for your suggestion Epy, although, I've implemented it (see below) and still get the same problem:

    Code:
    #include <stdio.h>
    # define MAXLINE 1000
    
    int getline(char[], int);
    void copy(char to[], char from[]);
    
    int main() /* print longest input line */
    {
    int len;
    int max;
    char line[MAXLINE];
    char longest[MAXLINE];
    
    max = 0;
    while ((len = getline(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 length */
    int getline(char s[], int lim)
    {
    int c, i;
    
    for (i = 0; i < lim - 1 && (c = getchar()) != 33 && c != 10; ++i)
        s[i] = c;
    if (c == 10) {
        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;}
    Following Dino, do you think that there is something wrong with my setup? I'm using Ubuntu 9.1 with C::B 8.02.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Kayl669
    I'm working through Kernighan and Ritchie's The C Programming Language and have copied example code (see below) into Code Blocks to improve my understanding of how it works. Unfortunately I'm getting the error: 'conflicting types for 'getline'...
    Rename getline to say, Kayl669_getline. There may be a getline function available via <stdio.h> that is coming into conflict with your code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Thanks laserlight; that's resolved the issue! Is there someway I can query my <stdio.h> to see what functions it contains?

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you are on linux, getline() is a GNU extension defined in stdio.h

    Line Input - The GNU C Library

    The only way to query stdio.h I can think of is grep:

    grep getline /usr/include/stdio.h
    extern _IO_ssize_t getline (char **__restrict __lineptr,
    Press any key to continue...


    "conflicting types" is a pretty good clue. If changing the name resolves that, then this is for sure the problem and just leave the name changed.
    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

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    OK. Thanks all. Very helpful. Dino I've sorted my mistype thanks for pointing it out.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try something like
    gcc -ansi prog.c

    Then you'll only have the ANSI interface to deal with, and not a lot of extra "helpful" functions declared for you by the compiler vendor.
    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. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM