Brooklyn
Brooklyn

Reputation: 17

Header file error: expected identifier or '(' before numeric constant

I'm new to header files and am not sure why I am getting this error. The first piece of code is from the relevant header file, and gives the expected identifier error:

#define MAX_ADDR_LENGTH 1000
struct listNode{
  char addr[MAX_ADDR_LENGTH];
  struct listNode *next;
};

Related to this, there is another error in the file relevant to that header, which gives me a "note: in expansion of macro 'MAX_ADDR_LENGTH', which it gives me on the line which gives the declaration of int MAX_ADDR_LENGTH:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"

char *crawl(char* getLinksFrom, int hopTo){

int MAX_ADDR_LENGTH = 300;
char startAddr[MAX_ADDR_LENGTH];
char destAddr[MAX_ADDR_LENGTH];

}

I've tried a number of things hoping it was just a small oversight (removed the #define altogther, deleted the line that gives the int MAX_ADDR_LENGTH declaration, just deleted the phrase 'int' from the same; all of which just caused even more errors).

Upvotes: 0

Views: 5476

Answers (2)

yacc
yacc

Reputation: 3361

Build your header like this:

#ifndef HEADER_H
#define HEADER_H

#define MAX_ADDR_LENGTH 1000
typedef struct _listNode{
  char addr[MAX_ADDR_LENGTH];
  struct _listNode *next;
} listNode;

#endif /* HEADER_H */

and use it like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "header.h"

char *crawl(char* getLinksFrom, int hopTo){

    char startAddr[MAX_ADDR_LENGTH];
    char destAddr[MAX_ADDR_LENGTH];
    .... more code
}

The #ifndef...#endif construct is called an include guard. It's not necessary to get your code compiled, but good practice to use it.

The typedef is used to define the node structure. It does not create a variable yet, it's just defining a type named listNode that you can use to build your list later on.

Upvotes: 0

Yaniv Shaked
Yaniv Shaked

Reputation: 758

The problem: ‘MAX_ADDR_LENGTH’ is defined twice in your code; Once as a Macro and once as a variable.

Try to delete the statement declaring MAX_ADDR_LENGTH as a variable.

Upvotes: 2

Related Questions