Using C struct without including header file

Posted by shams on Stack Overflow See other posts from Stack Overflow or by shams
Published on 2010-06-02T16:38:09Z Indexed on 2010/06/02 16:43 UTC
Read the original article Hit count: 188

Filed under:
|
|
|

My basic problem is that I want to use some structs and functions defined in a header file by not including that header file in my code.

The header file is generated by a tool. Since I don't have access to the header file, I can't include it in my program.

Here's a simple example of my scenario:

first.h

#ifndef FIRST_H_GUARD
#define FIRST_H_GUARD
typedef struct ComplexS {
   float real;
   float imag;
} Complex;

Complex add(Complex a, Complex b);

// Other structs and functions
#endif

first.c

#include "first.h"

Complex add(Complex a, Complex b) {
   Complex res;
   res.real = a.real + b.real;
   res.imag = a.imag + b.imag;
   return res;
}

my_program.c

// I cannot/do not want to include the first.h header file here
// but I want to use the structs and functions from the first.h
#include <stdio.h>

int main() {
   Complex a; a.real = 3; a.imag = 4;
   Complex b; b.real = 6; b.imag = 2;

   Complex c = add(a, b);
   printf("Result (%4.2f, %4.2f)\n", c.real, c.imag);

   return 0;
}

My intention is to build an executable for my_program and then use the linker to link up the executables. Is what I want to achieve possible in C?

© Stack Overflow or respective owner

Related posts about c

    Related posts about struct