tags:

views:

147

answers:

3

This is variation of one of exercises from Kernighan C book. Basically prints the longest line from standard input.

My question is, if looking at the 'copy method', how does the copy method actually work if it returns void. I'm coming from Java and learning C for the first time. In the copy method, the to and from char arrays are local. How do they actually get stored in longest and line variables in the main method if they are in no way returned back to main?

I am very confused...

Thanks very much for the time!

EDIT: THANK YOU FOR REPLIES. One more note... Oh. So you the author is passing the values with pointers. That is extremely confusing since the a page before it reads - "...in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy." Is that correct? Second question is, how can I make these functions pass data around just like in Java, PHP and etc. Or is this something C programmers see as a benefit?

#include <stdio.h>
#define MAXLINE 1000

int getline(char line[], int maxline);
void copy(char to[], char from[]);

main () {

 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;

}

int getline (char line[], int limit) {

 int i, c;

 for (i = 0;  i < limit - 1 && (c = getchar()) != EOF && c != '\n';  i++) line[i] = c;

 if (c == '\n') {

  line[i] = c;
  i++;

 }

 line[i] = '\0';

 return i;

}

void copy(char to[], char from[]) {

  int i;

  i = 0;
  while((to[i] = from[i]) != '\0') i++;

}
A: 

Hi. First of all, this is horrible code that you are learning from. It is hard to read, hard to understand, and doesn't handle lines longer that a given limit. Ick.

But to answer your question, when copy() is called, the two arguments are pointers to the regions in memory where the character arrays are stored. So when copy is called, the variable "to" equals "longest" in the calling frame, and "from" equals "line" in the calling frame.

vy32
For learning, it's better to start of with arbitrary limits than to try to explain pointers, `malloc()` and `free()` before even getting into the basics of string copying. In my humble opinion, anyway.
Chris Lutz
Oh. So you the author is passing the values with pointers.That is extremely confusing since the a page before it reads - "...in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy." Is that correct?Second question is, how can I make these functions pass data around just like in Java, PHP and etc. Or is this something C programmers see as a benefit?
Also, Kernighan's book is apparently described as a "Bible to C programming" by many professors and programmers. So, I stuck with this book.
The C function passes the _pointer_ by _value_ so you can't modify the pointer itself. However, you are free to modify the objects that the pointer _points to_ which is the basis for using pointers in the first place. You can declare your parameter as `const char *` instead of `char *` and then the compiler won't allow you to change the contents of the string, but then you can't copy into the new string - you can only read from the string.
Chris Lutz
It was certainly the bible in the 1980s...
vy32
+3  A: 

In C, everything is passed by value. You have to pay attention what is passed by value, though.

A C-style array like char from[] is not a container object, like you might expect from other languages. For most practical purposes, a C array is equivalent to a pointer to the first element, i.e. char * from.

So, the parameters to and from do get passed by value, but what they are is pointers to the data, so you can modify the data pointed to through them.

DevSolar
Thanks. Too bad Kernighan completely failed to mention this important fact.
caf
paxdiablo
phoebus
The nice turnaround is that you can use any pointer to a sequence of data objects like an array, including ptr[1] etc.
DevSolar
And I got this from ISO/IEC 9899:1999, not K-)
DevSolar
+1  A: 

Whereas Java strings are immutable, in C "strings" are merely arrays of characters. By changing the characters inside the array, the changes are 'visible' outside copy().

Tal Pressman