Like Tree1Likes

Character Arrays - 1.9 in K&R. need help

This is a discussion on Character Arrays - 1.9 in K&R. need help within the C Programming forums, part of the General Programming Boards category; Hi everyone, I'm stuck on section 1.9 in the K&R book. My main problem is that I copied the code ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    6

    Character Arrays - 1.9 in K&R. need help

    Hi everyone,

    I'm stuck on section 1.9 in the K&R book.

    My main problem is that I copied the code from the book verbatim and it's not printing the string for me. I'm not getting any errors when I compile. When I run the program I type in some lines and hit control-D to trigger EOF, and it doesn't print the string from the 'longest' array - printf("%s", longest);



    other confusion with arrays as function arguments:

    in the 'copy' function, to[] and from[] are in the function prototype, but their length is never defined, as opposed to line[] and longest[], which are specified in main()

    also it is confusing to me how getline() has (char line[], int maxline) in its prototype, but calls (char s[], and int lim) in its definition. Where as copy() uses the same arguments for its prototype and its definition.


    many thanks for any help!
    Nick

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    Post your code ... be sure to use code tags.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    When I run the program I type in some lines and hit control-D to trigger EOF, and it doesn't print the string from the 'longest' array - printf("%s", longest);
    CTRL D might not be EOF on your computer OS. When I typed it into a command prompt in windows, I got an error. If you're on windows you want CTRL Z.

    in the 'copy' function, to[] and from[] are in the function prototype, but their length is never defined, as opposed to line[] and longest[], which are specified in main()

    also it is confusing to me how getline() has (char line[], int maxline) in its prototype, but calls (char s[], and int lim) in its definition. Where as copy() uses the same arguments for its prototype and its definition.
    Yeah, parameter names can be different in the prototype and the definition. It's legal C. I suspect that the authors wanted to simply demonstrate this, but don't be confused. As you know now, having the names be different isn't helpful.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by NickDesigner View Post
    ...also it is confusing to me how getline() has (char line[], int maxline) in its prototype, but calls (char s[], and int lim) in its definition. Where as copy() uses the same arguments for its prototype and its definition.
    Parameter names are optional in a function prototype; getline() prototype can be just written as:
    Code:
    int getline(char, int)
    Only constraint is that the prototype has to agree with the definition and uses of the function.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Matticus View Post
    Post your code ... be sure to use code tags.
    Especially since we don't all have the K&R book...

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Here is the code:

    Code:
    #include <stdio.h>
    #define MAXLINE 1000
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    /*prints longest input line */
    int main()
    {
    
    	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);
    	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;
    	
    	while((to[i] = from[i]) != '\0')
    		++i;
    }

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by NickDesigner View Post
    Hi everyone,
    My main problem is that I copied the code from the book verbatim and it's not printing the string for me. I'm not getting any errors when I compile. When I run the program I type in some lines and hit control-D to trigger EOF, and it doesn't print the string from the 'longest' array - printf("%s", longest);
    Use CTRL Z for windows. Also note in order to see the output you might want to read How to stop my Windows console from closing - FAQ

    Quote Originally Posted by NickDesigner View Post
    other confusion with arrays as function arguments:

    in the 'copy' function, to[] and from[] are in the function prototype, but their length is never defined, as opposed to line[] and longest[], which are specified in main()
    In the copy function, the length is not specified because the assumption is made in the code that to to[] array is as long as the from[] array. See the comment you pasted right above the function:
    Code:
    /* copy:  copy 'from' into 'to'; assume to is big enough */
    The function knows when to stop because it checks for the end of the string, aka '\0' in the from[] array:
    Code:
    while((to[i] = from[i]) != '\0')
    Also note that your compiler should be complaining about i being used without being initialized. In the copy function you would want to have: int i=0;

    Quote Originally Posted by NickDesigner View Post
    also it is confusing to me how getline() has (char line[], int maxline) in its prototype, but calls (char s[], and int lim) in its definition. Where as copy() uses the same arguments for its prototype and its definition.
    As was pointed out above, the prototype and the definition are allowed to have different variable names. In fact most prototypes you will see will just define the type and not provide a name, aka your prototype would be:
    Code:
    int getline(char [], int);
    The authors most likely did this so you knew what variables were being passed to the function in main - did you see how the names matched the names of variables defined in main? Whereas in the function definition they used the names of the variables that would be used in the function - different than in main() although that is allowed - so as not to confuse the reader.

    K&R is a great reference although it can be hard to digest especially for someone just starting out in programming. Let me point you towards the immortal knowledge of Steve Summit, who in his wisdom provided a companion guide to the K&R series. C Programming.

    Pair these notes up with K&R and you should be good to go. Additionally, another good reference by the same man is here for all those odd ball questions.

    -Happy Coding
    Last edited by AndrewHunter; 07-23-2011 at 09:07 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Thank you so much! It was in fact that missing i = 0; in copy() that was the problem. I'm running on a Mac so control-D is the EOF command. Now that I made that fix it works, but the funny thing is my compiler didn't give me an error before. :/

    I have also just recently discovered the Steve Summit site, and I've been using this site which has the answers to the exercises, though I've found some of them hard to understand due to lack of explanation of the code. For example, their solution to Exercise 1-17 uses pointers even though we haven't seen them yet in the book.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    6

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by NickDesigner View Post
    Thank you so much! It was in fact that missing i = 0; in copy() that was the problem. I'm running on a Mac so control-D is the EOF command. Now that I made that fix it works, but the funny thing is my compiler didn't give me an error before. :/
    Using a variable uninitialized isn't a compile time error, but if your warning levels are turned up enough it would warn you about it.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by NickDesigner View Post
    The C Programming Language Answers To Exercises
    C'mon now... no cheating allowed.

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by quzah View Post
    Using a variable uninitialized isn't a compile time error, but if your warning levels are turned up enough it would warn you about it.
    Quzah.
    You should always set your compiler up to "treat warnings as errors". Let us know what compiler you are using and we can tell you how to do this to prevent future problems like this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    That would be great - I'm using Xcode 3.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    So your compiler most likely is gcc then since my understanding was Xcode was just an IDE. Either way take a look at this article I found. It appears to walk you through the process. For gcc you would be looking for -pedantic, -Werror and -wall options.
    Last edited by AndrewHunter; 07-28-2011 at 04:20 PM. Reason: ~
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Jul 2011
    Posts
    6
    Wow I didn't realize there were such advanced options for compiler warnings. Thanks you for the article, I will dive into this right away.

    Another quick question about simple programming logic:

    As far as I am understanding, a program runs through its code in a certain order, so when it reaches a called function it doesn't move on until that function finishes executing and possibly returns a value. So in the case of the code above, when user input is involved, does the program sort of halt at (c = getchar()) and only continue through the for loop when getchar() returns a value and sets it equal to c?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character arrays
    By enkwiringmindz in forum C Programming
    Replies: 8
    Last Post: 02-10-2010, 12:28 AM
  2. Character Arrays
    By JHaney in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2005, 12:53 PM
  3. Character Arrays
    By Smiley27 in forum C++ Programming
    Replies: 15
    Last Post: 04-13-2004, 04:19 PM
  4. Character arrays
    By PsychoBrat in forum C++ Programming
    Replies: 7
    Last Post: 06-21-2002, 12:02 PM
  5. help with character arrays
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 05:13 PM

Tags for this Thread


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