2016-05-13 9 views
0

**>他の構造体からの変数にアクセスする方法はありますか?ときCで構造体の変数にアクセスして印刷するにはどうすればいいですか?

i try this code,i am getting this compile error. ** test.c: In function ‘readRecordsFromFile’: test.c:70:18: error: expected expression before ‘kdnode’ printf(" %f\n",kdnode.data.latitude);

#include <stdio.h> 
#include <string.h> 
#include <stdbool.h> 
#include <stdlib.h> 
#include <math.h> 
#define rec_size 112 
typedef struct _Node kdnode; 
typedef struct _Record record; 

static void readRecordsFromFile(char *filename); 
struct _Record{ 
    int plateNumber; 
    long *name[32]; 
    double area; 
    int population; 
    int density; 
    int popcitycenter; 
    long region; 
    double latitude; 
    double longtitude; 
}; 
struct _Node 
    { 
     //kdnode left; 
     //kdnode right; 
     record data; 
     bool type; 
     double x; 
     double y; 
     int pagenumber; 
    }; 
    int main(){ 
    readRecordsFromFile("data.dat"); 
     return 0; 
    } 

    static void readRecordsFromFile(char *filename) 
    { 
     FILE* inputFile; 
     inputFile = fopen(filename, "rb"); 
     int i; 
     if(!inputFile) { 
     printf("Could not open file"); 
     return; 
    } 
     int length,record_count; 
     fseek(inputFile,0,SEEK_END); 
     length=ftell(inputFile); 
     fseek(inputFile,0,SEEK_SET); 
     record_count = length/sizeof(record); 
     kdnode kd; 
     fread(&kd,rec_size,2,inputFile); 
     printf("%d",ftell(inputFile)); 
     for (i = 0; i < record_count; i++) 
     { 
     printf(" %f\n",kdnode.data.latitude); 
     } 
      fclose(inputFile); 
    } 
+2

コードを読みやすくするために、コードを慎重にフォーマットしてください。 –

答えて

4

typedef struct _Nodeknodeとしてtypedef編です。 knodeは、データ型を表し、それは識別子ではありませんので、この

printf(" %f\n",kdnode.data.latitude); 

printf(" %f\n", kd.data.latitude); 

にする必要がありますが、例えばfread()のような関数の戻り値をチェックする必要があります。

関連する問題