In the below program, I am confused about one thing. When the getline function is called, it passes a char array of size 1000 by VALUE. It must have passed by value because there is no pointer or reference in the argument list of the getline function definition. And if that's the case, when exiting the getline function, isn't the s[] char array destroyed? And if it is destroyed, then when we reference line in the copy function, what are we actually copying?
#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 */
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;
i = 0;
while ((to[i] = from[i]) != '\0')
++i;
}
Actually, you aren't copying by value, you ARE copying by pointer (though you probably don't realize it).
Basically, array[] is the same as *array. The [] operator is simply a faster way to access variables, as the other way involves pointer arithmetic and can lead to wierd results if you forget about order of operations. The other way is like this:
1 2 3
array[1] = 5;
// is the same as
*(array + 1) = 5;
So what does this mean? In general, an array is simply a pointer to the first point in memory that was set aside for your data values. That means that you aren't actually passing by reference, because of course, your functions are exactly identical to this:
1 2
int getline (char* line, int maxline);
void copy (char* to, char* from);
To add to that, an array is exactly the same as a pointer, except that it knows its size. Once you degenerate to a pointer, you lose even that information.
I tried to run this program. First I had to change getline to another name to avoid naming conflicts with c library. But another thing is on bash shell I do this:
1 2
gcc -o longest-line longest-line.c
./longest-line
And basically it turns into a running process and the cursor just blinks. In the code, when the program is run and the getline function is called, it does 1000 iterations and the getchar is called each time to get input from the terminal in order to increment the counter if it's not end of file or newline. However, immediately there is no input in the terminal and when I start adding input and press the enter key:
1 2 3
$ ./longest-line
Hello World
Hello Again
Nothing happens. Isn't this supposed to print the longest line?