9

I want to declare a structure in a header file so I can use it in my source file. What am I doing wrong? I want to be able to access my struct from any function.

info.h

#ifndef INFO_H
#define INFO_H

typedef struct info
{
   int mem_size;
   int start_loc;
   int used_space;
   int free_space;
} INFO;
#endif

test.c

#include <stdio.h>
#include <stdlib.h>
#include <info.h>

#define F_first 1
#define F_last 2
#define F_data_int 3
#define F_data_char 4
#define F_data_float 5
#define F_print 6

void * f(int code);

int main() {

INFO in;
in.mem_size = 8;
f(F_last, 0,0);
return(0);
}

void * f(int code) {
printf("%d", in.mem_size);
}
4

2 に答える 2

9

Replace:

#include <info.h>

with,

#include "info.h"

With <> compiler only searches for the header file in predesignated header folder. This is used for standard library header files.
With "" compiler first searches the header file in the local directory where your .c file is located. This is used for user defined header files.

于 2013-02-05T03:13:27.567 に答える
1

Yes... You must use #include "info.h" as opposed to #include <info.h"> for custom based headers. Usually they are not part of the system branch which are normally located in the /usr/include directory on Unix/Linux platforms.

于 2013-02-05T05:01:58.640 に答える