-1

私はC言語に不慣れです。そこで、BrianKernighanとDennisRitchieの本(Cプログラミング言語)からコードをコピーしました。pg32の例をコピーしていました。このエラーが発生したとき:

pg32.c: In function ‘int main()’:
pg32.c:16:23: error: type mismatch with previous external decl of ‘char longest []’ [-fpermissive]
pg32.c:7:6: error: previous external decl of ‘char longest [1000]’ [-fpermissive]
pg32.c: In function ‘int getl()’:
pg32.c:33:20: error: type mismatch with previous external decl of ‘char line []’ [-fpermissive]
pg32.c:6:6: error: previous external decl of ‘char line [1000]’ [-fpermissive]
pg32.c: In function ‘void copy()’:
pg32.c:50:20: error: type mismatch with previous external decl of ‘char line []’ [-fpermissive]
pg32.c:6:6: error: previous external decl of ‘char line [1000]’ [-fpermissive]
pg32.c:50:31: error: type mismatch with previous external decl of ‘char longest []’ [-fpermissive]
pg32.c:7:6: error: previous external decl of ‘char longest [1000]’ [-fpermissive]

コードを4回再入力しましたが、同じエラーです。よくわかりませんが、これはレガシーな問題だと思います。

#include<stdio.h>

#define MAXLINE 1000

int max;
char line[MAXLINE];
char longest[MAXLINE];

int getl(void);
void copy(void);

int main()
{
  int len;
  extern  int max;
  extern char longest[];

  max =0;
  while((len =getl()) > 0)
    if(len > max){
      max = len;
    copy();
    }

  if(max > 0)
    printf("%s", longest);
  return 0;
}

int getl(void)
{
  int c, i;
  extern char line[];

  for(i =0; i < MAXLINE-1 &&
    (c=getchar()) != EOF && c != '\n'; ++i)
    line[i] =c;

  if(c=='\n'){
    line[i] = c;
    ++i;
  }
  line[i] ='\0';
  return i;
}

void copy(void)
{
  int i;
  extern char line[], longest[];

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

助けていただければ幸いです。

4

1 に答える 1

2

You have "line" and "longest" declared as global variables at the top of the file as fixed length arrays. Then you redeclare them as extern (no need for this by the way) as arrays of indeterminate length. Compiler is not liking that, so there are 2 solutions:

  1. Change every extern line[] to extern line[MAXLINES] and every extern char longest[] to extern char longest[MAXLINES]
  2. Remove all of your extern declarations and just use the globals at the top
于 2012-04-13T02:50:02.370 に答える