简体   繁体   中英

C function return statement

Currently learning C with 2nd Edition K&R book. I am confused with this example and how the value of my line variable is altered to the string from my getlines function.

int main()
{
  char line[MAXLINE];
  int found = 0;

  while (getlines(line, MAXLINE))
    printf("Test: %s\n", line);
}

int getlines(char s[], int lim)
{
  int c, i;
  i = 0;

  while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
    s[i++] = c;

  if (c == '\n')
    s[i++] = c;
  s[i] = '\0';
  return i;
}

The program outputs "Test: [string from getlines]". If I never assigned "char line" to the return value from getlines and the fact that getlines returns an int , how did the value of "line" get assigned the string that I input into the variable "s"?

You are apparently assuming that the function getlines() returns the string. It does not.

It returns the number of characters which were read into the line. The line[] variable in main is passed as a parameter to getlines which processes that parameter as variable s[] . Notice how it assigns characters to s[i++] .

getline() populates line when you passed it as an argument. The int returned is the status of the function after execution.

In a C function call, all arguments are passed by value , so the function has a copy of them. Therefore it's never possible that the function changes an argument. Sounds like contradicting what you observe here, but it isn't:

Arrays in C are somewhat special. An array identifier is evaluated to the address of the array's first element (aka: a pointer) in almost any place (exceptions include the sizeof operator). It's impossible to assign an array, and it is impossible to pass an array (as a function argument or as a return value).

Again, this looks contradicting, but there's another rule for function declarations: If a function declaration includes an array type , this is automatically adjusted to the corresponding pointer type . So your function

int getlines(char s[], int lim);

is in fact the following function: *)

int getlines(char *s, int lim);

The effect of adjusting the type in a function declaration and evaluating the identifier of an array as a pointer together is often expressed as the array decays as a pointer . What really gets passed to your function is a pointer, not an array. Indexing that pointer, the code in the function accesses the original array. The pointer is of course passed by value and your function doesn't change this pointer, but it can change what it points to.


*) stylistic note: I prefer to always write the function declarations with pointer types , because this is what the compiler sees anyways, so it avoids confusion. Of course, some argue it's good to write the array type as it documents the intent that this function should be passed a pointer to an array .

If I never assigned "char line" to the return value from getlines

Correct, you never assign the return value from getlines to the variable line .

how did the value of "line" get assigned the string that I input into the variable "s"

When you do getlines(line, MAXLINE) , the variable line although declared as an array " decays " to a pointer to the first element of the array when being passed. Then, in getlines when you are doing s[i++] = c; , you are actually writing the address (or some number of hops away from that address) that was passed to getlines . Remember that indexing an array s[i++] does an implicit dereference to i hops away from the address contained in s . Then, when you print line in your main function, you find that some information has been placed in the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM