コンパイラエラーの解決
Q&A
解決したいこと
人口密度の上位10都道府県を出力するプログラムを作成しています。コンパイルすると今まで出てきたことのないエラーが出てきます。調べてみると全角の空白がある場合に出るエラーとなっていましたが、理解ができず、その後進めません。プログラムを完成させる上で、エラーの状況を理解したいと考えます。解決方法をご教示お願いします。
発生している問題・エラー
出ているエラーメッセージを入力
ex_07_01.c: In function 'print_data':
ex_07_01.c:15:3: error: stray '\343' in program
printf(" %s %lf\n",p->name, (double)p->popu*10000/(double)p->area);
^
ex_07_01.c:15:3: error: stray '\200' in program
ex_07_01.c:15:3: error: stray '\200' in program
ex_07_01.c: In function 'sort_by_density':
ex_07_01.c:33:1: error: stray '\343' in program
for (i=0; i<n; i++) {
^
ex_07_01.c:33:1: error: stray '\200' in program
ex_07_01.c:33:1: error: stray '\200' in program
ex_07_01.c:35:25: error: 'prefecture' has no member named 'density'
if (data[j]->density > data[j-1]->density)
^
ex_07_01.c:35:46: error: 'prefecture' has no member named 'density'
if (data[j]->density > data[j-1]->density)
^
ex_07_01.c: At top level:
ex_07_01.c:42:1: error: expected identifier or '(' before '}' token
}
^
ex_07_01.c: In function 'main':
ex_07_01.c:63:4: error: unknown type name 'sort_by_'
sort_by_ density(N, data_p);
^
ex_07_01.c:4:11: error: expected declaration specifiers or '...' before numeric constant
#define N 47
^
ex_07_01.c:63:21: note: in expansion of macro 'N'
sort_by_ density(N, data_p);
^
ex_07_01.c:63:24: error: expected declaration specifiers or '...' before 'data_p'
sort_by_ density(N, data_p);
^
例)
NameError (uninitialized constant World)
```
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
#include <stdio.h>
#include <string.h>
#define N 47
#define SIZE 256
typedef struct {
int id;
char name[SIZE];
int area;
int popu;
} prefecture;
void print_data(prefecture *p) {
printf(" %s %lf\n",p->name, (double)p->popu*10000/(double)p->area);
}
prefecture set_data(int id, char name[], int area, int popu) {
prefecture data;
data.id = id;
strcpy(data.name, name);
data.area = area;
data.popu = popu;
return data;
}
void sort_by_density(int n, prefecture *data[]) {
prefecture *tmp;
int i, j;
for (i=0; i<n; i++) {
for (j=n-1; j>i; j--){
if (data[j]->density > data[j-1]->density)
tmp = data[j];
data[j] = data[j-1] ;
data[j-1] = tmp;
}
}
}
}
int main(void) {
FILE *fin;
prefecture data[N], *data_p[N];
char file[SIZE], name[SIZE];
int area, i, id, popu;
scanf("%s", file);
if ((fin = fopen(file, "r")) == NULL) {
printf("Can't open the file.\n");
return 1;
}
for (i=0; i<N; i++) {
fscanf(fin, "%d %s %d %d",&id, name, &area, &popu);
data[i] = set_data(id, name, area, popu);
data_p[i] = &data[i];
}
sort_by_ density(N, data_p);
for (i=0; i<10; i++) print_data(data_p[i]);
fclose(fin);
return 0;
}
自分で試したこと
全角の空白があるとの事だったので、エラーが出ている付近を削除してみた。
0