Thread: array problem

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    187

    array problem

    there code i dont understand 1 line in it i dont understand
    Code:
    #include <stdio.h>
    #define MAXLINE 1000 /* maximum input line length */
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    /* print the longest input line */
    int main(void)
    {
        int len; /* current line length */
        int max; /* maximum length seen so far */
        char line[MAXLINE]; /* current input line */
        char longest[MAXLINE]; /* longest line saved here */
        max = 0;
        while ((len = getline(line, MAXLINE)) > 0)
             if (len > max) {
                 max = len;
                 copy(longest, line);}
             if (max > 0) /* there was a line */
                 printf("%s", longest);
        getchar();
        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!='\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;
    }
    is bigger than zero means when he reached EOF but EOF is -1 right shouldnt that be -1 not 0 ?also the int c in 2nd loop why its using s[i]=c and he didnt in the for loop set it anything to the array s ?
    Last edited by elwad; 04-22-2009 at 07:40 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Greater than zero is used to determine if the line has valid data on it, or is just a blank line.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Even though these functions are from K&R, remove this:
    Code:
    != EOF && c
    Or, simply replace the && operator to ||. Beginners should not be using EOF in a console.

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    there code i dont understand 1 line in it i dont understand
    Code:
    #include <stdio.h>
    #define MAXLINE 1000 /* maximum input line length */
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    /* print the longest input line */
    int main(void)
    {
        int len; /* current line length */
        int max; /* maximum length seen so far */
        char line[MAXLINE]; /* current input line */
        char longest[MAXLINE]; /* longest line saved here */
        max = 0;
        while ((len = getline(line, MAXLINE)) > 0)
             if (len > max) {
                 max = len;
                 copy(longest, line);}
             if (max > 0) /* there was a line */
                 printf("%s", longest);
        getchar();
        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!='\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;
    }
    is bigger than zero means when he reached EOF but EOF is -1 right shouldnt that be -1 not 0 ?also the int c in 2nd loop why its using s[i]=c and he didnt in the for loop set it anything to the array s ?
    what's actually is ur problem?max is the length of the the line which will always be either greater than or equal to 0(in the case the line doesn't have any character).

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by abraham2119 View Post
    Even though these functions are from K&R, remove this:
    Code:
    != EOF && c
    Or, simply replace the && operator to ||. Beginners should not be using EOF in a console.
    you absolutely do not know what you are talking about...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I use tests for EOF in lots of my console functions and programs.

    You will definitely want to use EOF when you're working with text files.

    You might also remember that while K&R are merely mortals and make mistakes, they are also blatantly brilliant with their work in the C language.

    I would hesitate so many times before I said someone should change something substantive, in their famous book.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    srry electricity was out my problem that i dont understand that getline function i understand some stuff but genrally i dont get its idea and K&R is a really great book i have just started c programming and it taught me alot of stuff.

  8. #8
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    srry electricity was out my problem that i dont understand that getline function i understand some stuff but genrally i dont get its idea and K&R is a really great book i have just started c programming and it taught me alot of stuff.
    the getline() function takes a line with as much as 999 characters in it and the condition is that as oon as u press EOF the line will be finished and also if '\n' is pressed the line will be changed and the last character of the lin ewill be '\n'.

  9. #9
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    Quote Originally Posted by BEN10 View Post
    the getline() function takes a line with as much as 999 characters in it and the condition is that as oon as u press EOF the line will be finished and also if '\n' is pressed the line will be changed and the last character of the lin ewill be '\n'.
    so in getline function s[i] = c it cr8tes the string but this check
    Code:
     if 
    (
    c == '\n') {s[i] = c;
    ++i;
    } why did he do this coz a string doesnt contain \n ?also in main
    Code:
    while ((len = getline(line, MAXLINE)) > 0)
    i thought EOF is -1 not bigger than zero ?

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    len refers to the length of the string, not whether the file pointer has reached the EOF.

    He wants to include the newline char, and (I believe), count it as another line of text. (++i).

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by elwad View Post
    so in getline function s[i] = c it cr8tes the string but this check
    Code:
     if 
    (
    c == '\n') {s[i] = c;
    ++i;
    } why did he do this coz a string doesnt contain \n ?also in main
    Code:
    while ((len = getline(line, MAXLINE)) > 0)
    i thought EOF is -1 not bigger than zero ?
    if u press "ENTER" the line will be changed so the last character pressed is '\n' so the loop breaks and the last character pressed is '\n' which is give by if(c=='\n').
    also len is the length of the the characters inputted by the user which will always be >=0.if the first character inputted is itself EOF then also len=0 not -1 coz -1 is the value of EOF, it doesn't give u the length of the line.

  12. #12
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    so when u make cntrl_z in console it will set len to -1 whish will exit loop ? or set it to 0 ?

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    also one more question why did we increment i after if(c=='\n') isnt this a new string it should add 0 to it before increment ?

  14. #14
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    when u make cntrl+z 'len' will be set to 0 coz i already told u len is the length of the line i.e how many characters does the line have. len=-1 is nothing(imagine can the length of any line be negative).
    If u press '\n' 'i' will get incremented coz '\n' is also of one character length, so the ++i is done.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  15. #15
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    Quote Originally Posted by elwad
    is bigger than zero means when he reached EOF but EOF is -1 right shouldnt that be -1 not 0 ?
    function is return i
    it isn't return EOF
    when (c=getchar()) ==EOF || c == '\n' it is not enter the loop and go to s[i] = '\0'; if it wasn't read any character (got an EOF from getchar), or it was '\n', i wasn't encreased and equal to zero, s[0] = '\0'; return 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array problem
    By TomBoyRacer in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2007, 11:35 AM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Need desperate help with two dimensional array problem
    By webvigator2k in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2003, 02:28 PM