Jump to content

[SOLVED] [C] scope of function's parameters


rubing

Recommended Posts

Hey, (corbin I assume  ;))

 

I am working through another example.  This is a small program composed of 2 functions which takes a standard input of many lines and determines the longest.  It then prints the longest.  I do not understand how the second function called copy is obtaining the character array for the longest line.  I am thinking that maybe there is something different about the scope of parameters in C from php, which makes this possible...i don't know. your wise counsel is appreciated! 

 

Here is the code:

 

#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 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;
}

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

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

Link to comment
https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/
Share on other sites

"(corbin I assume)"

 

hehehe ;p

 

 

Seems like gets() (or fgets with stdin since a buffer over flow would be less likely then) would be easier to use than that getline nonsense....

 

Anyway, I don't see what your issue with the scope is....

 

 

if (len > max) {

      max=len;

      copy(longest,line);

}

 

 

longest is a variable in the scope of main(), and so is line.  What copy essentially does is set longest equal to line.

 

while ((to=from)!='\0')

    ++i;

 

 

When ever you do x=y in C (or most languages) the value returned is y.  So, that's basically saying:

 

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

I don't understand.

 

In this code snippet there is a function called getline which accepts 2 parameters:

 

line : which is defined as character array, but is initially empty

MAXLINE : which is the max length of a line for our testing purposes [1000 characters; prevents overflow]

 

getline returns the number of characters in the line.

 

It should not alter either of the parameters MAXLINE or line, except within the scope of that function.  Am I incorrect?

 

 

  while ((len=getline(line,MAXLINE)) > 0)
    if (len > max) {
      max=len;
      copy(longest,line);
    }

Oh!  Apparently I entirely misunderstood your first post, which is weird since your first post is fairly straight forward.  Guess I was out of it last night.

 

 

Anyway, arrays passed by value in C or C++ automatically transform into pointers.

 

 

In otherwords, the line variable in getline is actually a pointer to the front of the block of memory that line points to.

 

 

In a practical sense, it's pretty much like passing by reference in PHP.

(Note that I've essentially used and will continue to use passing a pointer and passing by reference interchangably, although they are technically not the same thing.)

 

 

If I remember right, it's possible with some magic to pass an array reference instead of pointer to a function, but that might just be possible in C++.  Don't remember.

 

 

 

Considering that it's not possible to return an array from a function (although you can return a pointer to the beginning of an array), I don't know when you wouldn't want to pass by reference.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.