2
#include <stdio.h>
#define MAXLINE 100

int get_line(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 so far*/
    char line[MAXLINE]; /*Current input line*/
    char longest[MAXLINE]; /*Longest line is saved here*/

    max = 0;
    while ((len = get_line(line, MAXLINE)) > 0){
        if (len > max){
            max = len;
            copy (longest, line);
        }
    }
    
    if (max > 0) 
        printf("%s", longest);

    return 0;
}

/*get_line: read a line into s, return length*/

int get_line(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'*/

void copy (char to[], char from[]){
       int i;
       i = 0;

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

This code is from Section 1.9 of The C Programming Language, I'm trying to understand how it works. I'm not understanding how the scopes of the variables work. In the while loop the function get_line is modifying the variable line and the function copy is modifying the variable longest.

Shouldn't they be only in the scope of the function? I don't get why the functions is able to modify them in main

  • 2
    You are mixing the scope of `from` with the content of the memory where it points to. Remember: When passing an array to a function, it decays to a pointer to the first element – Gerhardh Aug 01 '21 at 09:52
  • FWIW, `for (i = 0; (i < lim - 1) && (c = getchar()) != EOF && (c != '\n'); ++i)` is ***horrible*** code. There's nothing gained but confusion from cramming that much into one line. Stuffing an assignment, a subtraction, and ***three*** comparisons into just the middle of the three `for()` loop expressions is literally too much for any human brain to parse at once. Human brains can accurately keep track of only 3 to 6 states/operations at any one time, and that middle expression uses ***five*** of them. That's one huge reason you're having trouble understanding what this code does. – Andrew Henle Aug 01 '21 at 10:53

1 Answers1

1

Array's names are actually a pointer to first element.
What [] operator does is same as *(array + n)

Edit: As pointed out in the comments, this might be confusing and misleading.
I recommend you taking a look at this question: Is an array name a pointer?

int a[10];
a[1] == *(a + 1) // true

get_line() and copy() has parameter type of char[], which is same as char*.
You passed memory address of line and longest to those functions.

Variables aren't completely inaccessible when out of scope,
you just can't reference them directly using names.
Normally function parameters are copy of original value passed to the function,
so changing them won't affect the original varaible.

But in this case you're passing memory address of a variable, and those functions are able to change value of array's elements through pointer.

WieeRd
  • 792
  • 1
  • 7
  • 17
  • 3
    _Array's names are actually a pointer to first element_ Not entirely sure what you mean by this, but arrays are not pointers, but arrays decay into pointers when passed into functions. If that's what you meant, could you please clarify in your answer? – mediocrevegetable1 Aug 01 '21 at 10:24
  • Hmm but they do work as pointers even as variables, which is why you can manually get value of element like `*(a+1)`. Unless using `sizeof` on them, in which case they will return value for entire array. How exactly do you want me to edit my answer? – WieeRd Aug 01 '21 at 10:28
  • 1
    I believe it decays into pointer then as well when you `+` it. I suppose it would be helpful if you clarify that arrays and pointers are not one and the same (because to me it seemed like you are saying that from the first sentence), but array to pointer decay means that when you pass an array as an argument, it acts like a pointer when used in the function. – mediocrevegetable1 Aug 01 '21 at 10:33
  • 2
    @mediocrevegetable1 I've added a link to question for this array-pointer stuff :) – WieeRd Aug 01 '21 at 10:43