TsukasaZX wrote:
Sanity? Haven't y'all learned by now that I'm [censored] insane? Wink
Sure, but even the insane should like to save themselves compiling time in order to have more free time to be insane!
Eh, maybe. I'm actually so used to manual terminal compiling that it takes very little time at all Razz (read: I'm stubborn and see no reason to use make files right now)

I'll probably use a make file when I get past this first chapter of my game because things will be getting a lot more complex from there on out.


[edit]
Okay, I give. How do I get global variables to work across multiple files? From what I've read, I can declare them as external in the auxiliary files but that means having to keep track of multiple instances of declaring global variables....

[edit 2]
Okay, so the route would be to use ifdef/ifndef and a header .h file to declare and define them in one called instance and extern them in all others?

[edit 3]
Okay, so I get how this works for simple integers and whatnot with the _DECL [type] [name] _INIT([value]); method but what about more complicated things like arrays and structs? How could I define them in a header.h file?

[edit 4]
Wait, I don't think this is kosher stuff... disregard the previous edit? I am confused even more now x.x
TsukasaZX wrote:
I'll probably use a make file when I get past this first chapter of my game because things will be getting a lot more complex from there on out.


Screw that crap, just use an IDE. If you are on Windows, use Visual C++ Express Edition. If you are on Linux, use Eclipse CDT (which will create and manage the makefiles for you)

Quote:
Okay, I give. How do I get global variables to work across multiple files? From what I've read, I can declare them as external in the auxiliary files but that means having to keep track of multiple instances of declaring global variables....


Header.h:

Code:
extern int g_whatever;
extern char* g_whatever2;


Source.c:

Code:
#include "header.h"
int g_whatever = 0;
char* g_whatever2 = 0;


Source2.c

Code:
#include "header.h"
int doSomethingStupid() {
    g_whatever = 10;
    g_whatever2 = "this sucks";
}


That said, you should never, ever use a global variable if you can avoid it (which you always can - so never use a global variable). What you probably want is a singleton pattern.
I have nothing to add to what Kllrnohj said, except to point out that he's clearly been fully indoctrinated into the Google coding style.
KermMartian wrote:
I have nothing to add to what Kllrnohj said, except to point out that he's clearly been fully indoctrinated into the Google coding style.


I actually don't have C++ readability in Google Sad (meaning I can't submit C++ code). So far the only C++ code I've written was in webkit - and holy crap does its style rules *SUCK*. http://www.webkit.org/coding/coding-style.html <- wtf Apple? Seriously, wtf is that crap? MAKE UP YOUR DAMN MINDS. Also, tabs are for indentation, not spaces. Tabs are superior in every way.

Although I'm surprised you aren't a One True Brace Style fan yourself.
Okay, that makes sense, but what would the format be for a struct? Copy the whole thing and extern it and all of the variables within it?

Also, what the heck is a Singleton Pattern? I'm reading the wiki article and I'm nothing but confused.
Kllrnohj: Oh, don't worry, I'm fully-opinionated on tabs and brace styling.
Tsukasa: It's basically a class that returns the same instance every time it's instantiated, which lets you re-use the same instance in multiple files, which is actually pretty awesome.
Is it something usable in procedural C? It looks like OOP-fu to me.
TsukasaZX wrote:
Okay, that makes sense, but what would the format be for a struct? Copy the whole thing and extern it and all of the variables within it?


It's the same format

Header

Code:
typedef struct context {
    int somevar;
    char *message;
} context_t;

extern context_t gContext;


Source

Code:
#include "header"
context_t gContext = {42, "Meaning of Life"};


Some other source

Code:
#include "header"
#include <stdio.h>
int main(int argc, char* argv[]) {
   printf("%s = %d\n", gContext.message, gContext.var);
   getc(stdin);
   return 0;
}


TsukasaZX wrote:
Also, what the heck is a Singleton Pattern? I'm reading the wiki article and I'm nothing but confused.


It is the pattern where there is a single instance of something (be it a struct, class, buffer, whatever) and everything references that same thing. It is like a global, but accessed through a method.

Singleton.h

Code:
typedef struct context {
    int somevar;
} context_t;

context_t* getContext();


Singleton.c

Code:
#include <stdlib.h>
#include "Singleton.h"
context_t* getContext() {
    static context_t *sContext = 0;
    if (!sContext)
        sContext = (context_t*) calloc(1, sizeof(context_t));
    return sContext;
}
Completely unrelated to my game, but it's a C question nonetheless.

Say I have a 2D char array of [n][100] dimension (that is, an array of n many "strings" of 100 characters). N is arbitrary. How would I go about storing array[n] (one full row of 100 characters) to another char variable?

My PHP experience continually hampers my ability to understand variable types `-`
Well, first of all, you'll want to use malloc() to reserve each of those N 100-character strings, no? I believe that even though C99 technically allows:
Code:
void myfunction(int n) {
char twodee[n][100];
[...stuff...]
}
that it is frowned upon. Anyway, pretend that you had the above, then you could do something like this:

Code:
//copy twodee[2][*] -> twodee[5][*]
    memcpy(twodee[5], twodee[2], 100*sizeof(char));


mempy is (destination, source, number_of_bytes).
KermMartian wrote:
Well, first of all, you'll want to use malloc() to reserve each of those N 100-character strings, no? I believe that even though C99 technically allows:
Code:
void myfunction(int n) {
char twodee[n][100];
[...stuff...]
}
that it is frowned upon. Anyway, pretend that you had the above, then you could do something like this:

Code:
//copy twodee[2][*] -> twodee[5][*]
    memcpy(twodee[5], twodee[2], 100*sizeof(char));


mempy is (destination, source, number_of_bytes).


And what if I want to copy to some other variable called "holdvar" instead of another row in the same variable? How would I go about initializing "holdvar" and would this process differ completely?
What is holdvar? char[100], I presume from the context? Since holdvar would be a char* and twodee is a char**, you'd need to copy from a char* to a char*. twodee[row] is a char*, so how about:


Code:
memcpy(holdvar, twodee[row], 100*sizeof(char))

Protip: magic numbers are bad. Use something like #define ROWLENGTH 100 and then use ROWLENGTH instead of 100 everywhere.
Figured it out before you posted. Also, I know Wink
TsukasaZX wrote:
Figured it out before you posted. Also, I know Wink
Excellent, glad you figured it out. Smile For future reference, if you're dealing with structs (not arrays), and you want to copy the entire contents of one struct to another, struct_a = struct_b will take care of copying every element.
KermMartian wrote:
TsukasaZX wrote:
Figured it out before you posted. Also, I know Wink
Excellent, glad you figured it out. Smile For future reference, if you're dealing with structs (not arrays), and you want to copy the entire contents of one struct to another, struct_a = struct_b will take care of copying every element.


Oh yes, I am very well aware of that nifty tip Smile
Excellent, then you learned it before me. Smile I didn't discover that handiness until I was writing a compiler, and I had to implement the feature that does that (which is fairly nontrivial, I should add).
TsukasaZX wrote:
Completely unrelated to my game, but it's a C question nonetheless.

Say I have a 2D char array of [n][100] dimension (that is, an array of n many "strings" of 100 characters). N is arbitrary. How would I go about storing array[n] (one full row of 100 characters) to another char variable?


Are you sure you want an array? A linked list would be good there.
Kllrnohj wrote:
TsukasaZX wrote:
Completely unrelated to my game, but it's a C question nonetheless.

Say I have a 2D char array of [n][100] dimension (that is, an array of n many "strings" of 100 characters). N is arbitrary. How would I go about storing array[n] (one full row of 100 characters) to another char variable?


Are you sure you want an array? A linked list would be good there.


It's part of a homework assignment where the professor explicitly stated to use an array.
What if you simply swap around the char* pointers, now that I think about it more, something like twodee[newrow] = twodee[oldrow]? The main problem there is that then old and new row would contain pointers to the same data, and you'd lose the newrow's old pointer. A swap would prevent you from losing pointers.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6  Next
» View previous topic :: View next topic  
Page 4 of 6
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement