Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare struct in header file [closed]

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);
}
like image 229
Nelson.b.austin Avatar asked Feb 05 '13 03:02

Nelson.b.austin


1 Answers

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.

like image 142
Alok Save Avatar answered Oct 29 '22 13:10

Alok Save