I've come across an issue and for the life of me I can't figure it out. I need some serious help confirming if i'm thinking in the right way.

But say I have 3 files.

TableLayout.c
Powers.c
Powers.h

Now TableLayout.c is a class that just takes a number from the user and lays out numbers fed to it by Powers.c.

Powers.c is a simple class that fleshes out functions defined in Powers.h (things like square, cubed, square root etc)

How would I make TableLayout.c talk with Powers.c? Is there a way to "instance" Powers.c in TabelLayout.c? I could find no good links on google and some examples left me very confused.

Much MUCH thanks to any help in advance.

I have no idea what you mean by 'classes' (are you really programming in C?), but in general you should put structures and function prototypes in your headers, then #include them into whatever headers or source files you need them in.

yeah I know, classes was a terrible word to use (there was this one example that has class in the code so I assumed that was somehow important... see what I mean when I say confusing examples)

My goal is to make one .c file use the fleshed out functions in the other .c file. I don't want to just include the header file and flesh out the functions again in TableLayout.c.

I mean if the only thing I could do was include Powers.h and then redefine the functions in TableLayout, what would be the purpose of having Powers.c? And I am required to use Powers.c so I assume that there must be a way for TableLayout.c to talk to Powers.C

I hope this explains it better.

The general method of using multiple *.c files is to put the function prototypes in a header file, then include that header file in all the *.c files that need the functions. If you look at a couple standard C header file that are supplied by your compiler you will see that they only contain #defines, typedefs, and function prototypes.

Here is a simple, brief example. You need to compile both *.c programs then link the two object files in order to get the executable program. Exactly how to do that depends on the compiler you are using.

// foo.h
extern void foo();
// foo.c
#include <stdio.h>
#include "foo.h"

void foo()
{
   printf("Hello World\n");
}
// main.c
#include "foo.h"
int main()
{
    foo();
}
commented: Nice explanation ! +1
commented: Good job +29

aaaah!

So I have to use extern in the header file to be able to use the fleshed out function in the other c file! This was very informative and helpful, thank you!

extern is not absolutely required, but does clarify it a bit IMO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.