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 ...

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.

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.

...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.

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);

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;

Originally Posted by NickDesigner

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.

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.

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.

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.

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?