nocturnalassail
nocturnalassail

Reputation: 3

C Function and Variable Scope from K&R

I am currently reading K&R this code is confusing me, especially when it comes to variable/function scope...

#include <stdio.h>
#define MAXLINE 1000

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

/*print the longest input line*/
void 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;
}

/*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;

}

How does the getline() function have the scope to save into the line[] array that is located in main()?

How does the copy() function have the scope to save into the longest[] array that is located in main()?

I can understand if the functions were nested inside of main(), but in this example they are not.

Upvotes: 0

Views: 154

Answers (5)

unwind
unwind

Reputation: 399889

The getline() function doesn't "have the scope" to store directly to line, but that doesn't matter since the address of line is passed to getline and becomes the address of the local s argument.

This means that while getline() is indeed writing to exactly the same memory as is used by line in main(), it's not aware of that. You'll notice that line is not mentioned by name inside getline().

Upvotes: 0

reza.safiyat
reza.safiyat

Reputation: 639

In C, if a function calls another function, passing the address of any of its variables to the called function, the called function may access and manipulate the data at that address any possible way it wants. Here, the address of char line[] is passed to the function getline() which enables getline to access and modify and fill line[].

If you're studying arrays and functions, you most definitely have used scanf() in the previous lessons. Ever wondered how scanf() can fill data into a variable defined inside main()? :-) After all, scanf() is a yet another function right? And now you may surely be able to explain why we add an & (address of operator) before an int or char variable when calling scanf(). We are just passing the function the address of the location where data is to be filled.

Upvotes: 1

haccks
haccks

Reputation: 106052

Basically you are passing a pointer to the first element of your arrays (declared in main) to your function. For more clarity prototypes could also be written as

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

When you call your function from main and pass array name as its argument then the array name converted to the pointer to its first element. So, what copied to the function parameter is the starting address of array, not the array itself. Now any changes to the passed address (i.e, array) is also reflected to main.

Upvotes: 1

Ishmeet
Ishmeet

Reputation: 1610

line is declared in main, and its storage is in the main only. inside your other functions i.e. copy and getline, the base address of the array is passed, like a pointer. Hence, copy and getline are able to use the address and access the array's which are define in your main.

Upvotes: 0

Sadique
Sadique

Reputation: 22821

How does the getline() function have the scope to save into the line[] array that is located in main()?

getline() gets the copied value of the address of line in the local variable char s[]

How does the copy() function have the scope to save into the longest[] array that is located in main()?

Because you are passing the address of the variable.

copy(longest, line); becomes copy(to, from) - so to say. The values of the base addresses are copied.

This has more to do with pointers and passing arguments than scope. You are confusing these concepts.

Upvotes: 0

Related Questions