K&R2 section 1.9 character arrays

A

arnuld

this is the example programme from that section. it does not print the
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:


/* K&R2 section 1.9 character arrays

example programme

STATEMENT: to take number of lines as input and print the longest line

*/


#include <stdio.h>

#define MAXLINE 1000 /* MAXIMUM length of input line */

int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);

int main() {

int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */

while((len_current = get_current_line(current_line, MAXLINE)) > 0)
{
if(len_current > max_seen)
{
max_seen = len_current;
copy_line(longest, current_line);
}
}

if(max_seen > 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}


return 0;
}



int get_current_line(char s[], int max_length)
{
int c, i;

for(i=0; i < (max_length - 1) && ((c = getchar()) != EOF) && c !=
'\n'; ++i)
s = c;

if(c == '\n')
{
s = c;
++i;
}

s = '\0';
return i;
}


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

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

}
 
A

arnuld

sorry, i forgot to tell:

i am using Arch Linux, gcc 4.1.2 using these options:

"gcc -std=c99 -Wall -Wextra input.c"

thanks
 
S

santosh

arnuld said:
this is the example programme from that section. it does not print the
longest line otherwise it works fine, my version has only 1 extra line
of "printf". i want to know why it is not printing the longest line:


/* K&R2 section 1.9 character arrays

example programme

STATEMENT: to take number of lines as input and print the longest line

*/


#include <stdio.h>

#define MAXLINE 1000 /* MAXIMUM length of input line */

int get_current_line(char line[], int maxline);
void copy_line(char to[], char from[]);

int main() {

int len_current;
int max_seen;
char current_line[MAXLINE]; /* current line */
char longest[MAXLINE]; /* longest line seen so far */

while((len_current = get_current_line(current_line, MAXLINE)) > 0)
{
if(len_current > max_seen)

Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
{
max_seen = len_current;
copy_line(longest, current_line);
}
}

if(max_seen > 0)
{
printf("Length of Longest line seen so far is: %d\n", max_seen);
printf("\n Longest line is: \n%s", longest);
}


return 0;
}



int get_current_line(char s[], int max_length)
{
int c, i;

for(i=0; i < (max_length - 1) && ((c = getchar()) != EOF) && c !=
'\n'; ++i)
s = c;

if(c == '\n')
{
s = c;
++i;
}

s = '\0';
return i;
}


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

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


Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.
 
A

arnuld

Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.

thanks Santosh, it works now.

tell me one thing. in the "copy_line" function:


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

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

}

the loop stops as soon as it encounters '\0'. if loop breaks at this
point, this means '\0' was *not* copied as next element. during
printing, we are using "%s" as argument which accepts an "array" and
an array end sin '\0' but our copied array does not have '\0'.

?
 
S

santosh

arnuld said:
Comparing to an uninitialised object: max_seen. Read the K&R example
again. Note that it initialises max_seen, (max in their version), to
zero before the while loop.
Using an uninitialised object: i. Note: in K&R, they initialise i to
zero before the loop.

thanks Santosh, it works now.

tell me one thing. in the "copy_line" function:


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

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

}

the loop stops as soon as it encounters '\0'.


Read the construct again.

The test against null character is made _after_ the copy has taken
place. The result of the subexpression is the value of the left
operand of the assignment operator. If it's a null character, (which
is already copied to the destination array), the loop breaks.
 
A

arnuld

Read the construct again.

i did and did not get.
The test against null character is made _after_ the copy has taken
place. The result of the subexpression is the value of the left
operand of the assignment operator. If it's a null character, (which
is already copied to the destination array), the loop breaks.

NOW i got. but i want to tell you one thing, it is........ GREAT.

i mean it looked difficult and alien to me but NOW i see it is so only
from "outside" but i think and feel a language which uses such *terse*
constructs will become my favourite. there is something behind C that
is powerful and has its own culture and following. i say so only after
you explained to me what that sentence is.

sorry for going a little OT and i am impressed (i always thought C is
quite old and ugly and will become a "pain in the ass" BUT my feelings
are changing now)


:)
 
S

santosh

thanks Santosh, it works now.

[ ... ] during printing, we are using "%s" as argument which accepts an "array" and
an array end sin '\0' but our copied array does not have '\0'.

The %s specifier expects an argument of type of const char *. It
interprets the contents beginning at the location held in the argument
as a string. Thus only a valid C string must be passed as an argument
to the %s conversion specifier.

A char array need not end in a '\0'. A string must be terminated by
one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top