Problem with Kernigan's example

I'm reading Kernigan's book about C lanuage and trying examples in it
All examples before works fine but this one..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#DEFINE  MAXLINE  1000 /* MAXIMUM INPUT
       LINE SIZE */
 MAIN()  /* FIND LONGEST LINE */
 {
 INT LEN; /* CURRENT LINE LENGTH */
 INT MAX; /* MAXIMUM LENGTH SEEN SO FAR */
 CHAR LINE[MAXLINE]; /* CURRENT INPUT LINE */
 CHAR SAVE[MAXLINE]; /* LONGEST LINE, SAVED */

 MAX = 0;
 WHILE ((LEN = GETLINE(LINE, MAXLINE)) > 0)
     IF (LEN > MAX) {
    MAX = LEN;
    COPY(LINE, SAVE);
     }
     IF (MAX > 0)   /* THERE WAS A LINE */
    PRINTF("%S", SAVE);
 }
 GETLINE(S,LIM) /* GET LINE INTO S,RETURN LENGTH */
 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(S1, S2)    /* COPY S1 TO S2;
               ASSUME S2 BIG ENOUGH */
 CHAR S1[], S2[];
 {
 INT I;

 I = 0;
 WHILE ((S2[I] = S1[I] != '\0')
    ++I;
 }

I have some questions same as compilator. )
1. Why main function is at top in program listing
2. Array LINE and SAVE defined only at function MAIN, how can other function can get access to them.
3. Kernigan tells variables can be pass to fucntion only as value not as link, so how can i alter value of array at any other function except MAIN where it was be defined.
Last edited on
2) You can define them above all (outside main) as external variables.
3) Using pointer as argument to function
strange Kernigan's book include wrong examples or C language chaged since Kernigan so much?
I am pretty sure the book you are using is at least 20 years old. main doesn't have a return value here, the functions are old K&R style functions... this example isn't valid anymore since the C90 standard was released. Which was, as you could probably already guess, in 1990. I don't know who gave you that book, but you should tell him to go a bit with the time.
I don't have a copy of the standard, but if I tell Code Blocks (GCC) to use ANSI C90 it does compile old style function declarations without even a warning. I think they were only deprecated in C99.
Last edited on
New style functions were introduced in C90 though, it would have made sense if they deprecated the old ones then.

Though I gotta admit that I don't have a copy of the standard myself, I doubt anyone would have still taught K&R style after C90, considering you can't create prototypes with them.
*sniff*
Stack at this example.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <stdio.h>
#define MAXLINE 100
using namespace std;
	char line[MAXLINE];
	char save[MAXLINE];

int Copy(char line[MAXLINE], char save[MAXLINE]){
	int i=0;
	extern	char line[MAXLINE];
	extern	char save[MAXLINE];

	while (line[i]=save[i]!='\0') 
		++i;
	return 0;
}

int GetLine(char line[MAXLINE],int lim){
	int c,i;
	extern	char line[MAXLINE];
	for (i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
		line[i]=c;
	if (c=='\n') {
		line[i]=c;
		++i;
	}
	line[i]='\0';
	return(i);
}

int main () {
	int len;	//current line length
	int max;	//max length seen so far
	int i;
	extern	char line[MAXLINE];
	extern	char save[MAXLINE];

	max=0;
	while ((len=GetLine(line,MAXLINE))>0) 
		if (len>max){
			max=len;
			Copy(line,save);
		}
		if (max>0) {
			for(i=0;i<MAXLINE;i++) {
				if (save[i]!='\0') cout <<save[i]<<endl;
			}
		}
	return 0;
}	//end of main 

it doesn't give any errors, but empty output. (
Can somebody help ?
Thanks at advance
SOLVED ) btw it was a good practice for gdb (was using it first time)
Btw can somebody advise me any good modern book for c++ ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <stdio.h>
#define MAXLINE 100
using namespace std;
	char line[MAXLINE];
	char save[MAXLINE];

int Copy(char line[MAXLINE], char save[MAXLINE]){
	int i=0;
	extern	char line[MAXLINE];
	extern	char save[MAXLINE];

	while (line[i]!='\n') {
		save[i]=line[i];
		i++;
	}
	save[i]='\n';
	return 0;
}

int GetLine(char line[MAXLINE],int lim){
	int c,i;
	extern	char line[MAXLINE];
	for (i=0;(i<lim-1) && ((c=getchar())!=EOF) && (c!='\n');++i)
		line[i]=c;
	if (c=='\n') {
		line[i]=c;
		i++;
	}
//	line[i]='\t';
	return(i);
}

int main () {
	int len;	//current line length
	int max;	//max length seen so far
	int i;
	extern	char line[MAXLINE];
	extern	char save[MAXLINE];

	max=0;
	while ((len=GetLine(line,MAXLINE))>0) 
		if (len>max){
			max=len;
			Copy(line,save);
		}
		if (max>0) {
			cout <<save;
		}
	return 0;
}	//end of main 
Last edited on
Topic archived. No new replies allowed.