Header files let you declare all the necessary stuff in one place and make it available to all the .c files that need it. You don't want to #include files with code because it become problematic in larger projects, you can end up compiling the same code twice. For that same reason, don't put any function bodies in .h files.
complesso.h
Code:
#ifndef COMPLESSO_H__ // these are include guards and should be in every header (.h) file
#define COMPLESSO_H__ // read the wikipedia article on include guards for more info
#define LENGTH 1023 // so we don't use "magic numbers"
// define the complex type here, so everybody can use it
typedef struct {
float real;
float img;
} complex;
void randomarray(complex *vettore); // function prototype
#endif // COMPLESSO_H__ -- this ends our include guard
complesso.c
Code:
#include <stdlib.h> // for calling rand()
#include "complesso.h" // so we can see the typedef and LENGTH
void randomarray(complex *vettore)
{
int i;
for (i = 0; i < LENGTH; i++) {
if (rand() % 2)
vettore[i].real = 1.0;
...
}
}
main.c
Code:
#include <stdio.h> // for printf()
#include <stdlib.h> // for srand()
#include <time.h> // for time()
#include "complesso.h" // now main.c knows what LENGTH is and the correct way to use randomarray()
int main(void)
{
srand(time(NULL)); // this goes in your program exactly once, at the very beginning
...
return 0;
}