
-----------------------------------
Srlancelot39
Mon Jul 14, 2014 8:59 am

Structures + pointers + functions + files + tutorial please!
-----------------------------------
I have found myself in a situation where I must read and write structures and their elements to/from functions across 3 different files.  I know (somewhat) how to do these things on their own, but combining all of these things together is not going very well lol.  If anyone could give me a miniature lecture on this, or even post a tutorial in the C Tutorials section, I would greatly appreciate it.

Thanks!

-----------------------------------
Insectoid
Mon Jul 14, 2014 10:09 am

RE:Structures + pointers + functions + files + tutorial please!
-----------------------------------
Structures are just organized groups of variables. You read and write to them the same as anything else, except you have to access the individual variables in the struct.

[code]
struct {
    a : int
    b : int
}c;

fprintf (c.a);
fprintf (c.b);
[/code]

When you dereference a pointer (with the * character), it's just a variable like any other.
[code]
int* a;
*a = 1;
fprintf (*a);[/code]

Functions (other than void ones) return a value, which can be treated like a variable.
[code]
int a () : int {
    return 1
}
fprintf (a());[/code]

All of these things are just tools. Anyone can use these tools one at a time, but that doesn't do anything useful. Programming is combining these tools together in a useful way, and that's the most important thing to learn how to do.

-----------------------------------
Srlancelot39
Mon Jul 14, 2014 10:47 am

RE:Structures + pointers + functions + files + tutorial please!
-----------------------------------
I understand how to do all that, my problem is with passing them to/from functions in different files.

-----------------------------------
Insectoid
Mon Jul 14, 2014 12:50 pm

RE:Structures + pointers + functions + files + tutorial please!
-----------------------------------
You mean separate source files? Do you understand how to use #include?

-----------------------------------
Dreadnought
Mon Jul 14, 2014 1:10 pm

Re: Structures + pointers + functions + files + tutorial please!
-----------------------------------
If you define your struct in a header file there should be no problem using the structure in many files.

Here's an example (although for something like numbers I would generally not use pointers)

complex.h
#ifdef __COMPLEX_H__
#define __COMPLEX_H__

struct Complex {
    float re;
    float im;
};

void Conjugate (Complex* c);

#endif //__COMPLEX_H__

complex.c
#include "complex.h"

void Conjugate (Complex* c) {
    c->im = -c->im;
}


main.c
#include "complex.h"

Complex* MakeComplex(float r, float i) {
    Complex* c = (Complex*)malloc(sizeof(Complex));
    c->re = r;
    c->im = i;
    return c;
}

void main() {
    Complex* c = MakeComplex(3,4);
    // c -> 3 + 4i

    Conjugate(c);
    // c -> 3 - 4i

    free(c);
}

Unfortunately I left home without my C compiler so I cannot guarantee that this will run as is (might be some semi-colon I missed or something).

-----------------------------------
Srlancelot39
Mon Jul 14, 2014 6:35 pm

Re: RE:Structures + pointers + functions + files + tutorial please!
-----------------------------------
You mean separate source files? Do you understand how to use #include?
I am making use of separate source files and headers, and yes I do :P

To go into more detail:
One of my issues is I am being asked to declare a structure and certain variables locally and use them project-wide.
I.e.

int main()
{
struct bridge truss

If you define your struct in a header file there should be no problem using the structure in many files.

My structure is defined in a header file, however, the specific instance of it (i.e. struct bridge truss[size]) is (and must be) declared in main.  I also have some variables that must be local yet accessed by another .c file.  I've been told there's a way to do this, I'm just not sure of the specifics required in doing so.

-----------------------------------
Insectoid
Mon Jul 14, 2014 8:00 pm

RE:Structures + pointers + functions + files + tutorial please!
-----------------------------------
If you have variables in main that need to be accessed by another file, they should be declared in a header (main.h, for example). Then you can just #include it anywhere you need it (with appropriate include guards, of course).

If it's declared inside the main() function, it won't be accessible by external files, as far as I know.

-----------------------------------
Dreadnought
Mon Jul 14, 2014 9:08 pm

Re: Structures + pointers + functions + files + tutorial please!
-----------------------------------

If you have variables in main that need to be accessed by another file, they should be declared in a header (main.h, for example). Then you can just #include it anywhere you need it (with appropriate include guards, of course).

If it's declared inside the main() function, it won't be accessible by external files, as far as I know.


Just to clarify, there is a difference between declaring a variable and defining a variable (
// ifdef stuff

extern int externalInteger;

//endif 

main.c
#include "header.c"

int externalInteger;

int main (void) {
  externalInteger = 42;
}

And you can do whatever you like with "externalInteger" in any other file that includes "header.h"

-----------------------------------
Srlancelot39
Tue Jul 15, 2014 8:09 am

Re: Structures + pointers + functions + files + tutorial please!
-----------------------------------
Okay, thanks.  I think I tried using extern, but I probably did it wrong or had some other error preventing my program from working.  I'll try it again.  My thoughts have been the same as Insectoid's though; it seemed impossible and impractical to me to declare it in main().

EDIT: Ah, I remember the issue I had with extern now...it has to be a local variable, and thus I cannot declare it outside of main().  Declaring it in main() and having extern in my header file results in an error since it can't find the variable.

EDIT2: I fixed it!  I was doing some variable assignments in the wrong places, which was making it impossible to do what I needed to do.  I also needed to add two new structure variables; one to hold the record temporarily (instead of using individual temporary variables) and return it to main() in another source file, and another to take on this returned structure and assign it to a location in the database.
