저는 책 "The C Programming Language"를 읽고 모든 예제를 수행하고 GDB에서 무슨 일이 일어나는지 살펴볼 것입니다.
아래 예제 코드에서 목표는 몇 줄의 텍스트를 평가하여 가장 긴 줄을 결정하는 것입니다. gdb가를 호출하는 for 루프에 걸려 있기 때문에 내 프로그램이 실패한다고 생각했습니다 getchar()
. 역 추적을 따라 범인 기능을 찾았지만 정확한 문제를 파악할 수 없었습니다. 그런 다음 예제 코드를 사용하여 똑같은 작업을 수행 for
했으며을 호출 하는 루프 에서 똑같은 문제가 발생 합니다 getchar()
.
// file: ch1/ex16.c
// OBJECTIVE: Revise the main routine o the lnogest program
// so it will correctly print the length of arbitrarily long
// lines and as much as possible of the text.
#include <stdio.h>
#define MAXLINE 1000
int getLine(char line[], int maxline);
void copy(char to[], char from[]);
int main()
{
int len, max;
char line[MAXLINE], 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 = 0;
while ((to[i] = from[i]) != '\0'){
++i;
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다