rubing Posted February 8, 2009 Share Posted February 8, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/ Share on other sites More sharing options...
rubing Posted February 8, 2009 Author Share Posted February 8, 2009 The character array line is changes in the getline function, which seems fine. But then in the if statement that follows that character array is being fed into copy, which seems like it should be outside its scope?? Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757187 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 "(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; Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757304 Share on other sites More sharing options...
rubing Posted February 8, 2009 Author Share Posted February 8, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757457 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757515 Share on other sites More sharing options...
rubing Posted February 8, 2009 Author Share Posted February 8, 2009 that's wierd...i'm not sure i like that :-\ Is there someone over at ANSI, I can complain to about this ??? Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757681 Share on other sites More sharing options...
corbin Posted February 8, 2009 Share Posted February 8, 2009 (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. Quote Link to comment https://forums.phpfreaks.com/topic/144271-solved-c-scope-of-functions-parameters/#findComment-757716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.