3

My c header file has the following error message in Xcode

Redefinition of 'entry'

But it works perfectly when I compile it using gcc in command line. Could any of you give an explanation of why?

This is snapshot.h:

#ifndef SNAPSHOT_H
#define SNAPSHOT_H

#define MAX_KEY_LENGTH 16
#define MAX_LINE_LENGTH 1024

typedef struct value value;
typedef struct entry entry;
typedef struct snapshot snapshot;

struct value {
    value* prev;
    value* next;
    int value;
};

// the line below is where the redefinition error appears
struct entry {
    entry* prev;
    entry* next;
    value* values;
    char key[MAX_KEY_LENGTH];
};

struct snapshot {
    snapshot* prev;
    snapshot* next;
    entry* entries;
    int id;
};

#endif

This is snapshot.c:

#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "snapshot.h"

int
main(int argc, char *argv[]){
    int x = 7;
    printf("x= %d\n" , x);
    printf("value = %d\n", 1);
    return 0;
}
hexinpeter
  • 1,470
  • 1
  • 16
  • 14

1 Answers1

6

entry was originally reserved as a keyword and then later declared obsolete. So older compilers don't allow it (see this question). Change the name of the struct and everything should be fine.

Community
  • 1
  • 1
Benjy Kessler
  • 7,356
  • 6
  • 41
  • 69