mgurcan
mgurcan

Reputation: 170

How can i access variables in structs in C to print?

**> Is there a way to access a variable that come from other struct? When

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);
    }

Upvotes: 0

Views: 63

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53006

typedef struct _Node is typedefed as knode. knode represents a data type and it's not an identifier, so this

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

has to be

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

You should also check return values for functions like fread() for example.

Upvotes: 4

Related Questions