プロが教える店舗&オフィスのセキュリティ対策術

インクルード 定義 メイン関数 エラー内容 が収まりませんでした;
(長すぎてどうすればよいのやら;)
int readlines(char *lineptr[], int maxlines)
{
int len, nlines;
char *p, line[MAXLEN];
nlines = 0;
while ((len = getline(line, MAXLEN)) > 0)
if (nlines >= maxlines || (p = alloc(len)) == NULL)
return -1;
else {
line[len-1] = '\0';
strcpy(p, line);
lineptr[nlines++] = p;
}
return nlines;
}
char *alloc(int n)
{
if (allocbuf + ALLOCSIZE - allocp >= n) {
allocp += n;
return allocp - n;
} else
return 0;
}
void kr_qsort(char *v[], int left, int right,
int (*comp)(void *, void *))
{
int i, last;
void swap(char *v[], int i, int j);
if (left >= right)
return;
swap(v, left, (left + right)/2);
last = left;
for (i = left+1; i <= right; i++)
if ((*comp)(v[i], v[left]) < 0)
swap(v, ++last, i);
swap(v, left, last);
kr_qsort(v, left, last-1, comp);
kr_qsort(v, last+1, right, comp);
}
void swap(char *v[], int i, int j)
{
char *temp;
temp = v[i];
v[i] = v[j];
v[j] = temp;
}
void readargs(int argc, char *argv[])
{
char c;
int atoi(char *);
while (--argc > 0 && (c = (*++argv)[0] == '-' || c == '+') {
if (c == '-' && !isdigit(*(argv[0]+1)))
while (c = *++argv[0])
switch (c) {
case 'd':
option |= DIR;
break;
case 'f':
option |= FOLD;
break;
case 'n':
option |= NUMERIC;
break;
case 'r':
option |= DECR;
break;
default:
printf("sort: illegal option %c\n", c);
error("Usage: sort -dfnr [+pos1] [-pos2]");
break;
}
else if (c == '-')
pos2 = atoi(argv[0]+1);
else if ((pos1 = atoi(argv[0]+1)) < 0)
error("Usage: sort -dfnr [+pos1] [-pos2]");
}
if (argc || pos1 > pos2)
error("Usage: sort -dfnr [+pos1] [-pos2]");
}
}
void writelines(char *lineptr[], int nlines, int order)
{
int i;
if (order)
for (i = nlines-1; i >= 0; i--)
printf("%s\n", lineptr[i]);
else
for (i = 0; i < nlines; i++)
printf("%s\n", lineptr[i]);
}
int charcmp(char *s, char *t)
{
char a, b;
int i, j, endpos;
int option, pos1, pos2;
int fold = (option & FOLD) ? 1 : 0;
int dir = (option & DIR) ? 1 : 0;
i = j = pos1;
if (pos2 > 0)
endpos = pos2;
else if ((endpos = strlen(s)) > strlen(t))
endpos = strlen(t);
do {
if (dir) {
while (i < endpos && !isalnum(s[i]) && s[i] != ' ' && s[i] != '\0')
i++;
while (j < endpos && !isalnum(t[j]) && t[j] != ' ' && t[j] != '\0')
j++;
}
if (i < endpos && j < endpos) {
a = fold ? tolower(s[i]) : s[i];
i++;
b = fold ? tolower(t[j]) : t[j];
j++;
if (a == b && a == '\0')
return 0;
}
} while (a == b && i < endpos && j < endpos);
return a - b;
}
int numcmp(char *s1, char *s2)
{
double v1, v2;
char str[MAXSTR];
substr(s1, str, MAXSTR);
v1 = atof(str);
substr(s2, str, MAXSTR);
v2 = atof(str);
if (v1 < v2)
return -1;
else if (v1 > v2)
return 1;
else
return 0;
}
void substr(char *s, char *str, int maxstr)
{
int i, j, len;
extern int pos1, pos2;
len = strlen(s);
if (pos2 > 0 && len > pos2)
len = pos2;
else if (pos2 > 0 && len > pos2)
error("substr: string too short");
for (j = 0, i = pos1; i < len; i++, j++)
str[j] = s[i];
str[j] = '\0';
}
int getline (char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
void error(char *s)
{
printf("%s\n", s);
exit(1);
}

A 回答 (4件)

まず最初にエラーメッセージを載せて, それから「エラーが出ている行」の周辺を書いた方が早いんじゃないかな.


というか, コンパイルエラーくらいならエラーメッセージを読んで自分で直せ.
    • good
    • 1
この回答へのお礼

> まず最初にエラーメッセージを載せて, それから「エラーが出ている行」の周辺を書いた方が早いんじゃないかな.

わかりました 参考にさせていただきます。

> コンパイルエラーくらいならエラーメッセージを読んで自分で直せ.
すみません・・・メッセージを見て考えても 難しくてわからないんです・・・。あと、周囲の人も自分のことでいっぱいいっぱいなんで、ここで聞かざるを得なくて・・、今後は質問を極力控えるようにします。

お礼日時:2009/02/17 17:15

readargs( )で ")" が足りないのと "}" が多いですよ。

    • good
    • 0
この回答へのお礼

アドバイスありがとうございます!
おかげで なんとかできそうです。

お礼日時:2009/02/17 17:07

インクルードしているヘッダファイルと、


独自に参照関数やdefine定義を行っているヘッダファイルの内容も載せてください。
    • good
    • 0
この回答へのお礼

補足に載せておきました!

お礼日時:2009/02/17 16:56

足りない部分を補足へ、載せて下さい。

この回答への補足

ああ、なるほど!その手がありましたね;!今更ですが;、先ほどのアドより、行番号の指摘があったので付けてみました。
1 #include <stdio.h>
2 #include <string.h>
3 #include <ctype.h>
4 #include <math.h>
5 #include <stdlib.h>
6 #define NUMERIC 1
7 #define DECR 2
8 #define FOLD 4
9 #define DIR 8
10 #define LINES 100
11 #define MAXSTR 100
12 #define MAXLEN 1000
13 #define ALLOCSIZE 10000
14 #define MAXLINES 5000
15 int charcmp(char *s, char *t);
16 void error(char *s);
17 int numcmp(char *s1, char *s2);
18 void readargs(int argc, char *argv[]);
19 int readlines(char *lineptr[], int maxlines);
20 void kr_qsort(char *v[], int left, int right,
21 int (*comp)(void *, void *));
22 void writelines(char *lineptr[], int nlines, int order);
23 void substr(char *s, char *str, int maxstr);
24 int getline (char s[], int lim);
25 char option = 0;
26 int pos1 = 0;
27 int pos2 = 0;
28 char *lineptr[MAXLINES];
29 char linestor[20];
30 char *alloc(int n);
31 static char allocbuf[ALLOCSIZE];
32 static char *allocp = allocbuf;
33 main(int argc, char *argv[])
34 {
35 char *lineptr[LINES];
36 int nlines;
37 int rc = 0;
38 readargs(argc, argv);
39 if ((nlines = readlines(lineptr, LINES)) > 0) {
40 if (option & NUMERIC)
41 kr_qsort((char **) lineptr, 0, nlines-1,
42 (int (*) (void *, void *)) numcmp);
43 else
44 kr_qsort((char **) lineptr, 0, nlines-1,
45 (int (*) (void *, void *)) charcmp);
46 writelines(lineptr, nlines, option & DECR);
47 } else {
48 printf("input too big to sort \n");
49 rc = -1;
50 }
51 return rc;
52 }
このあと、上に掲載したint readlines(char *lineptr[], int maxlines)へとつながります。
エラー内容は、
kadai5-17.c: 関数 `readargs' 内:
kadai5-17.c:102: error: conflicting types for `atoi'
/usr/include/stdlib.h:144: error: previous declaration of `atoi'
kadai5-17.c:102: 警告: `atoi' のextern 宣言はグローバルのそれと一致しません
kadai5-17.c:103: error: 文法エラー before '{' token
kadai5-17.c:110: error: case label not within a switch statement
kadai5-17.c:113: error: case label not within a switch statement
kadai5-17.c:116: error: case label not within a switch statement
kadai5-17.c:119: error: `default' label not within a switch statement
kadai5-17.c:109: error: break statement not within loop or switch
kadai5-17.c:112: error: break statement not within loop or switch
kadai5-17.c:115: error: break statement not within loop or switch
kadai5-17.c:118: error: break statement not within loop or switch
kadai5-17.c:122: error: break statement not within loop or switch
kadai5-17.c: トップレベル:
kadai5-17.c:124: error: 文法エラー before "else"
です。ここまで見て下さり 本当に感謝です!

補足日時:2009/02/17 16:55
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!