# Beginning C character array explanation [closed]

I have been following along the book 'The C Programming Language' 2nd edition by Kernighan and Ritchie. The below code is an exercise from the book. What I am confused about in this code is the Character array 'line'. It is a local variable so I don't understand how the value is changing when it is used as an argument in the 'get_line' function.

/*******************************
* Author:                     *
* Date: 4/22/2019             *
* Purpose: Book exercise      *
*******************************/

#include <stdio.h>
#include <stdlib.h>

#define MAX_LINE 1000

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

/*Print longest input line*/
int main()
{
int len;    //Current line length
int max;    //Maximum length seen so far

char line[MAX_LINE];        //Current input line
char longest[MAX_LINE];     //Longest line saved here

max = 0;

//While input length is greater than 0
while((len = get_line(line, MAX_LINE)) > 0)
{
//If current string is longer than max
if (len > max)
{
max = len;              //Assign new max value
copy(longest, line);    //Replace longest string value with current string
}
}

if (max > 0)
{
printf("%s", longest);
}
return 0;
}

int get_line(char s[], int lim)
{
int c, i;

/*i = 0; i less than limit - 1 *and* input is not EOF *and* input is not enter key */
for (i=0; i<lim - 1 && (c=getchar())!=EOF && c!='\n'; i++)
{
s[i] = c; // input array position i is = input
}

//Once enter key is hit
if (c == '\n')
{
s[i] = c;   //Add enter key '\n' onto array
i++;        //Incr i so that last char can be '\0' later
}

s[i] = '\0';    //The last spot on char array is '\0'
return i;
}

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

i = 0;

//Assign from[i] to to[i] until null terminator
while((to[i] = from[i]) != '\0')
{
i++;
}
}


For example if I had a variable in the main routine

int add_num(int num1);
main()
{
int x = 5;
printf("%d", x);
}

{
num1 += 5;
return num1;
}


x would still remain equal to 5 unless I did:

x = add_num(x);


then x would equal 10.

So how does the line character array change when the function that it's used in returns 'i'? Where I is the length of the string entered.

Sorry if this was a little long, but I wanted to explain myself clearly.

## closed as off-topic by pacmaninbw, Vogel612♦Apr 23 at 15:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Authorship of code: Since Code Review is a community where programmers improve their skills through peer review, we require that the code be posted by an author or maintainer of the code, that the code be embedded directly, and that the poster know why the code is written the way it is." – pacmaninbw, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.

• This question is off-topic for 2 reasons, the first is it isn't asking for a code review it is asking a "How To Question". The second reason is that you aren't the author of most of the code. Please see "How to I ask a good question?" and "What types of questions should I avoid asking" at codereview.stackexchange.com/help. – pacmaninbw Apr 23 at 15:05
• Unfortunately this question doesn't reflect what the site is about. We review code that you have written for improvements. It's not on topic to ask for explanations of code that has been written by someone other than you. For more information, see the help center. Thanks. – Vogel612 Apr 23 at 15:15