Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Structures + pointers + functions + files + tutorial please!
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Srlancelot39




PostPosted: Mon Jul 14, 2014 8:59 am   Post subject: 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!
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Mon Jul 14, 2014 10:09 am   Post subject: 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);


When you dereference a pointer (with the * character), it's just a variable like any other.
code:

int* a;
*a = 1;
fprintf (*a);


Functions (other than void ones) return a value, which can be treated like a variable.
code:

int a () : int {
    return 1
}
fprintf (a());


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




PostPosted: Mon Jul 14, 2014 10:47 am   Post subject: 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




PostPosted: Mon Jul 14, 2014 12:50 pm   Post subject: RE:Structures + pointers + functions + files + tutorial please!

You mean separate source files? Do you understand how to use #include?
Dreadnought




PostPosted: Mon Jul 14, 2014 1:10 pm   Post subject: 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
c:
#ifdef __COMPLEX_H__
#define __COMPLEX_H__

struct Complex {
    float re;
    float im;
};

void Conjugate (Complex* c);

#endif //__COMPLEX_H__


complex.c
c:
#include "complex.h"

void Conjugate (Complex* c) {
    c->im = -c->im;
}


main.c
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




PostPosted: Mon Jul 14, 2014 6:35 pm   Post subject: Re: RE:Structures + pointers + functions + files + tutorial please!

Insectoid @ Mon Jul 14, 2014 12:50 pm wrote:
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 Razz

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.
c:

int main()
{
struct bridge truss[arraysize]; //this needs to be accessed by another .c file.
int this_must_be_local; //this needs to be accessed by another .c file
//more code
}


Dreadnought @ Mon Jul 14, 2014 1:10 pm wrote:
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




PostPosted: Mon Jul 14, 2014 8:00 pm   Post subject: 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




PostPosted: Mon Jul 14, 2014 9:08 pm   Post subject: Re: Structures + pointers + functions + files + tutorial please!

Insectoid wrote:

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 (or at least wikipedia says so).
When you define a variable the compiler sets aside memory for it and so forth.
When you declare a variable the compiler does not allocate memory but requires that the variable be defined elsewhere.

So, indeed, here we want to declare the (external) variable in the header file (otherwise each file will define its own variable).

In C this is done using the extern keyword.

For example:

header.h
c:

// ifdef stuff

extern int externalInteger;

//endif 


main.c
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"
Sponsor
Sponsor
Sponsor
sponsor
Srlancelot39




PostPosted: Tue Jul 15, 2014 8:09 am   Post subject: 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.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: