易时最长行复制程序 [英] Easy C longest line copying program
问题描述
这是从Kernighan的本C语言书中的一个练习的变化。基本上打印从标准输入线最长。
我的问题是,如果在看复制方法,如何复制方法实际上,如果它返回void工作。我从Java到来,学习C的第一次。 在复制法,并从字符数组是本地的。他们怎么实际上得到存储在主方法最长,行变量,如果他们在没有办法返回到主?
我很迷茫......
非常感谢的时候了!
的编辑:谢谢您的答复的
。还有一记...
哦。所以,你笔者与指针传递值。这是因为极其混乱的一个页面之前,它读取 - ......在C调用的函数不能直接改变调用函数的变量,它只能改变其私有的临时副本。这是否正确?
第二个问题是,如何才能让这些功能各地传递数据,就像在Java中,PHP等或者是这个东西C程序员看到的有什么好处?
从K&放code; R
的#include<&stdio.h中GT;
#定义MAXLINE 1000INT函数getline(焦线[],INT MAXLINE);
无效副本(字符为[],在[]字符);主(){ INT LEN;
INT最大;
焦线[MAXLINE]
烧焦最长[MAXLINE] 最大= 0; 而((LEN =函数getline(线,MAXLINE))大于0){ 如果(LEN>最大){ 最大= LEN;
副本(最长的,行); } } 如果(最大值大于0)的printf(%S,最长); 返回0;
}INT函数getline(焦线[],INT限制){ INT I,C; 对于(i = 0; I<限 - 1安培;!及(C =的getchar())= EOF和放大器;和C ='\\ n';我++)线由[i] = C; 如果(C =='\\ n'){ 线[I] = C;
我++; } 线[I] ='\\ 0'; 返回我;
}无效副本(字符为[],在[]字符){ INT I; I = 0;
而((在[I] =从[I])!='\\ 0')
我++;
}
在C,一切按值通过。你必须要注意的什么是按值传递,虽然。
一个C数组像从[] CHAR
不是一个容器对象,就像你可能会从其他语言的期望。最实用的目的,C数组就相当于一个指向第一个元素,即的char *从
。
因此,参数到
和从
确实因价值获得通过,但它们是什么指针的数据,这样你就可以修改数据指出,通过他们。
This is variation of one of exercises from Kernighan C book. Basically prints the longest line from standard input.
My question is, if looking at the 'copy method', how does the copy method actually work if it returns void. I'm coming from Java and learning C for the first time. In the copy method, the to and from char arrays are local. How do they actually get stored in longest and line variables in the main method if they are in no way returned back to main?
I am very confused...
Thanks very much for the time!
Edit: Thank you for the replies.
One more note...
Oh. So you the author is passing the values with pointers. That is extremely confusing since the a page before it reads - "...in C the called function cannot directly alter a variable in the calling function; it can only alter its private, temporary copy." Is that correct?
Second question is, how can I make these functions pass data around just like in Java, PHP and etc. Or is this something C programmers see as a benefit?
Code from K&R
#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 line[], int limit) {
int i, c;
for (i = 0; i < limit - 1 && (c = getchar()) != EOF && c != '\n'; i++) line[i] = c;
if (c == '\n') {
line[i] = c;
i++;
}
line[i] = '\0';
return i;
}
void copy(char to[], char from[]) {
int i;
i = 0;
while((to[i] = from[i]) != '\0')
i++;
}
In C, everything is passed by value. You have to pay attention what is passed by value, though.
A C-style array like char from[]
is not a container object, like you might expect from other languages. For most practical purposes, a C array is equivalent to a pointer to the first element, i.e. char * from
.
So, the parameters to
and from
do get passed by value, but what they are is pointers to the data, so you can modify the data pointed to through them.
这篇关于易时最长行复制程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!