user3043594
user3043594

Reputation: 166

While Loop Char Array Passed By Reference

So I'm trying to determine the longest line for a school assignment. Here is the code I think is of question. I don't understand why the "longest_line" prints fine in the while loop, but won't print outside the loop.

    int main() {
  int ch;
  char line[MAXLINE] = { '\n' };           /* current input line */
  char longest_line[MAXLINE]  = { '\n' };   /* copy of longest line */

  while ((ch = readline(line, MAXLINE)) != EOF) {
    if (longest(line, longest_line))
      copy(longest_line, line);
    printf("Read Line: %s\n", line);
    printf("Long Line: %s\n", longest_line);
  }

  printf("Print Long Line: %s\n", longest_line);

Here is the output:

calc L
copy
Read Line: sdafasdfsadfsadfs
Long Line: sdafasdfsadfsadfs
calc L
Read Line: sadfs
Long Line: sdafasdfsadfsadfs
calc L
Read Line: sdfasdfsafd
Long Line: sdafasdfsadfsadfs
calc L
Read Line: sadfsdf
Long Line: sdafasdfsadfsadfs
calc L
Read Line: sadfs
Long Line: sdafasdfsadfsadfs
calc L
Read Line: 
Long Line: sdafasdfsadfsadfs
calc L
Read Line: sfsafsdfsf
Long Line: sdafasdfsadfsadfs
Print Long Line: 

It's like the longest_line gets erased after the loop, which doesn't make sense to me. Sorry I'm a newb. Thanks to the guy who knows the technical reason behind this anomaly.

Here is my entire program if you wish to run it: (I fixed the variables)

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>

#define MAXLINE 80   /* maximum input line size */

/* function declarations */
int readline( char line[], int max );
void copy( char to[], char from[] );
bool longest( char read_line[], char previous_line[] );

/* print longest input line */

int main() {
  int ch;
  char line[MAXLINE] = { '\n' };           /* current input line */
  char longest_line[MAXLINE] = { '\n' };   /* copy of longest line */

  while ((ch = readline(line, MAXLINE)) != EOF) {
    if (longest(line, longest_line))
      copy(longest_line, line);
    printf("Read Line: %s\n", line);
    printf("Long Line: %s\n", longest_line);
  }

  printf("Print Long Line: %s\n", longest_line);

    return 0;
}

/* readline: read a line into s, return length */
int readline(char s[], int lim) {
  int i = 0; // the current index 
  char c;    // the current char

  c = getchar();
  while (c != '\n' && i < lim) {
    s[i] = c;
    i++;
    c = getchar();
  }
  s[i] = '\0';  // final charachter of string

    return c;
}

/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[]) {
  int i;
  for (i = 0; to[i] = from[i]; ++i);
printf("copy\n");
}

/* longest: Which line is the longest */
bool longest( char read_line[], char previous_line[] ) {
  printf("calc L\n");
  char ch;
  int size_read = 0;
  int size_previous = 0;

  // determine length of read line
  ch = read_line[size_read];
  while (ch != '\0') {
    ch = read_line[size_read];
    ++size_read;
  }

  // determine length of previous line
  ch = previous_line[size_previous];
  while (ch != '\0') {
    ch = previous_line[size_previous];
    ++size_previous;
  }

  // determine longest line
  if (size_read > size_previous)
    return true;
  else
    return false;
}

Upvotes: 1

Views: 127

Answers (1)

NPE
NPE

Reputation: 500713

I won't give the details away, but there are several places in the code where you fail to initialize variables, leading to undefined behaviour.

Upvotes: 3

Related Questions