Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Apparently I don't know as much C as I thought... | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 1964/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-03-04 10:23 AM Link | Quote
I managed to get all the window-creating code working, but no window actually appears. Do I have to actually show it? Also I was hoping to have a way to bring up a console window (via code, so it could be toggled) to print debug info to via printf(). Is this possible? If not is there a way (using VC++ 6) to print to the debug info in the IDE? Finally the most troubling problem - how do I put variables into strings? In most cases, I would do 'printf("variable = %d",variable);", but if I want to use something like MessageBox() I can't do that, because the comma separates parameters and messes it up. (Whenever I try to use strcpy(), strcat() etc it just crashes.)

[edit] Remember, kids, always remember to make your windows visible.


(edited by HyperHacker on 11-03-04 01:36 AM)
Euclid

Cheep-cheep
Level: 23

Posts: 118/193
EXP: 65528
For next: 2195

Since: 03-15-04
From: Australia

Since last post: 24 days
Last activity: 7 days
Posted on 11-03-04 03:00 PM Link | Quote
Yeah i would think it's something along the lines of ShowWindow for the striked out question

the second question i'm not too familiar with.

as for your most troubling problem, I would make a big array of temp char[] to store your "new string" (using something along the lines of sprintf or strcat, your choice), then throw that string onto the messagebox, but that requires a bit of work... or you could try a console, which i have no idea how to bring it up. Another way is to output the messages to a file then look at it, use things like fopen so you can get fprintf working on it.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 1965/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-03-04 10:44 PM Link | Quote
The VB6 IDE has a window where you can type code and execute it, and print debug info from the app. I was hoping there was something similar for debug logging. If nothing else, bringing up a console window to printf to. (I don't need the code execution stuff. )
Dish

Spiny
Level: 38

Posts: 162/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-04-04 03:08 AM Link | Quote
Originally posted by HyperHacker
I managed to get all the window-creating code working, but no window actually appears. Do I have to actually show it?


If you want to save a ShowWindow() call, you can or WS_VISIBLE with your window styles to make the window visible as soon as it's created.

WS_VISIBLE | WS_OVERLAPPEDWINDOW <-- will give you a lot of the typical window features and will be visible right away.


Also I was hoping to have a way to bring up a console window (via code, so it could be toggled) to print debug info to via printf(). Is this possible? If not is there a way (using VC++ 6) to print to the debug info in the IDE?


I'm not sure of a way, actually. What I would recommend is creating a log file or something on startup then write strings to it with fprintf():

#include <stdio.h>
FILE* pDebugFile = fopen("C:\\debuglog.txt","wt");
fprintf( pDebugFile, "This will be put in the debug file\n" );
fclose( pDebugFile );

fprintf can do all the string formatting that printf does (%d, %X, and all that good stuff)


Finally the most troubling problem - how do I put variables into strings? In most cases, I would do 'printf("variable = %d",variable);", but if I want to use something like MessageBox() I can't do that, because the comma separates parameters and messes it up. (Whenever I try to use strcpy(), strcat() etc it just crashes.)


printf() <--- prints to the default iostream (I think... not quite sure exactly how printf works)
fprintf() <--- does same, but writes to a file
sprintf() <--- does same, but writes to specified buffer (this is what you want)

char some_string[80];
sprintf( some_string, "This will be put in some_string" );

again.. same string formatting that printf does
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 1984/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-06-04 08:06 AM Link | Quote
I got the windows working. Just had to make it visible.

I've tried sprintf() but it just crashes. Am I doing it right? sprintf(TargetString,"Variable is %d",Variable); should do it, I would think...
Cellar Dweller

Flurry
!!!
Level: 27

Posts: 171/269
EXP: 107817
For next: 8342

Since: 03-15-04
From: Arkansas

Since last post: 16 days
Last activity: 34 min.
Posted on 11-06-04 09:31 AM Link | Quote
It should not crash if TargetString is big enough. Could you post the declaration of TargetString, or, better yet, the whole function or source file?

I would also suggest using snprintf().


(edited by Cellar Dweller on 11-06-04 12:39 AM)
sloat

Level: 16

Posts: 10/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 11-06-04 10:21 AM Link | Quote
I'm pretty sure there are macros for VC++ that let you print to the debug output window if debugging is enabled.

I don't remember what they are...I've never used them...but I'm pretty sure I saw them somewhere in MSDN. That's not much help, I know. I do remember that the names were weird and you needed some obscure header.

But you should just forget about all that fancy debugging stuff. Everything you need is provided by the Beep and MessageBox API functions.


(edited by sloat on 11-06-04 01:22 AM)
Dish

Spiny
Level: 38

Posts: 171/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-06-04 11:53 AM Link | Quote
HyperHacker: Yeah that should be working. Just make sure your TargetString is a large enough buffer to hold the entire string (plus null terminator).

Cellar Dweller: What is snprintf()? And why do you recommend it over tried-and-true sprintf()? my MSDN searches came up with nothing, and it doesn't seem to be part of stdio (didn't work when I tried to compile it). I'm of the impression that "if it ain't broke, don't fix it"... and sprintf certainly isn't broke.

sloat: while it's true that you won't NEED a debugger... it can really make the difference when tracking down those really nasty buggers. Simple breakpoints alone are a wonder at tracking down exactly where your flaw is on a program crash... and variable viewing is beyond invaluable (you -could- print the vars in question in a messagebox... but seeing the result of one var may warrant you to check another... and without a debugger you'd have to stop, recompile, re-run, etc.. and in the process tacks a lot of code that can be really ugly when you forget to remove it all --which I've done in the past XD)
Cellar Dweller

Flurry
!!!
Level: 27

Posts: 172/269
EXP: 107817
For next: 8342

Since: 03-15-04
From: Arkansas

Since last post: 16 days
Last activity: 34 min.
Posted on 11-07-04 09:13 AM Link | Quote
Originally posted by Disch
What is snprintf()? And why do you recommend it over tried-and-true sprintf()? my MSDN searches came up with nothing, and it doesn't seem to be part of stdio (didn't work when I tried to compile it). I'm of the impression that "if it ain't broke, don't fix it"... and sprintf certainly isn't broke.


After less than a minute with Google I found this and this.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 1994/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-07-04 12:40 PM Link | Quote
Originally posted by Disch
HyperHacker: Yeah that should be working. Just make sure your TargetString is a large enough buffer to hold the entire string (plus null terminator).
...Hey, it worked.

Sloat: Lol! I hadn't thought of using Beep() for debugging.

Alright, now for the next problem. (Gotta love C. ) I have a .CPP file in which a variable is declared (HWND hWindow). I also have 2 header files, main.h and cpu.h. In main.h, I declare function prototypes so that they can be accessed from other files. (Why they can't by default is beyond me, but whatever. ) This works alright, but the other files (cpu.h) can't access the variable. In main.h, I have 'extern HWND hWindow;' to make it accessible, but it doesn't seem to be working.


(edited by HyperHacker on 11-07-04 03:44 AM)
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 1620/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 11-07-04 01:30 PM Link | Quote
Originally posted by Disch
Cellar Dweller: What is snprintf()? And why do you recommend it over tried-and-true sprintf()? my MSDN searches came up with nothing, and it doesn't seem to be part of stdio (didn't work when I tried to compile it). I'm of the impression that "if it ain't broke, don't fix it"... and sprintf certainly isn't broke.
it is broken in one sense, it doesn't do bounds checking and a majority of viruses possible because of buffer overruns and what function do they hijack: printf/sprintf but anyways, unless you are writting an operating system--you can get by just using the traditional versions of printf/sprintf

HH: function prototypes allow the compiler to use a function before the function has been defined, they don't allow a function in a.c to access a function in b.c if b.c uses a function prototype.

as for not being able to access hWindow, does cpu.h include main.h? i believe if you want to reference a variable not declared in your file, it is in that file you declare 'extern'
Dish

Spiny
Level: 38

Posts: 174/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-07-04 11:26 PM Link | Quote
Cellar Dweller: for some reason I didn't think to search google. Thanks for the links, even though I found your post to be somewhat condescending



Originally posted by HyperHacker
I also have 2 header files, main.h and cpu.h. In main.h, I declare function prototypes so that they can be accessed from other files. (Why they can't by default is beyond me, but whatever. )


What you have to understand, is that the compiler does not compile the project. It compiles a single .cpp/.c file. Therefore, it is impossible for the compiler to know a function exists unless it comes across the declaration while it's parsing the file.

You can think of #include lines as copying the included file and pasting it in the .cpp file right at the #include line.

From what I'm understanding of your problem... you're attempting to use a variable before it's declared. For example:

somevar += 4; //this will generate errors, since somevar isn't defined until the next line
int somevar;

what you're probably donig in your program is similar (mind you I'm guessing... correct me if I have it wrong):

---------
#include "main.h"
#include "cpu.h"

HWND hWindow;
--------

Now.... if you think back to what I said about #including being kind of a copy/paste deal... anything in main.h and cpu.h is found by the compiler BEFORE hWindow is declared... so hWindow is invisible in those files. If you switch things around and put the hWindow declaration above the #include lines, then it will solve your problem (note: for this .cpp file only! see below). But this is sloppy sloppy sloppy. If you have functions in main.h and cpu.h that work with a window... pass the HWND to the function as a parameter. Do not rely on global variables for this kind of thing.

Now... another problem associated with global vars is, while they work well in a single .c/.cpp file, complications occur when there's several .cpp files that want to use it. For example... if you compiled two or more .cpp files, both of which have 'HWND hWindow;' declared, they would compile just fine... but when you link them together, you'll get errors (this is a similar error to what you would get if you give the same function two bodies in two different .cpp files... the reason behind the errors is similar, too).

This is where the whole 'extern' business comes in. 'extern' isn't like a magic keyword to make global vars work... what it really does is tells the compiler "do not actually make this variable because it will be filled in by the linker". This is the same kind of logic as declaring functions without a body. If it would help to understand, you can think of 'extern' variables as body-less variables.. and normal variables as having a body. If you can understand why it's problematic for a single function to have two bodies, then conceptually, you should be able to understand why it's problematic to have a single variable with two bodies.


But again... I still recommend against using global vars. If you need a variable in another function, it's usually better to pass the variable in question as a parameter to the function rather than having them all use the same globals.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2012/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-08-04 03:13 AM Link | Quote
Hmm... That could work, with some creative coding...
Is there by any chance a way to declare arrays with bounds that don't start at 0? Like Array1[0 to 49], Array2[50 to 99], etc. That would save me a lot of math work.
Dish

Spiny
Level: 38

Posts: 176/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-08-04 03:53 AM Link | Quote
I'm not sure exactly what you're getting at...

You could do 2-dimensional arrays.. like for example:

int some_array[4][50];

here, some_array[1][49] would be identical to/interchangable with some_array[0][99]


Or if you're just looking to change the origin (which I don't see why you'd need to), you could do a pointer trick:

int some_array[50];
int *some_ptr = some_array - 50;

here, some_array[0] would be the same as some_ptr[50] (changing your origin from zero to 50). But this is bad practice.... since it will produce an extra indirection (slightly slower) and I think compilers are smart enough to optimize something like "some_array[ index - 50 ]" so that the implied subtraction never really takes place.
HyperLamer
<||bass> and this was the soloution i thought of that was guarinteed to piss off the greatest amount of people

Sesshomaru
Tamaranian

Level: 118

Posts: 2019/8210
EXP: 18171887
For next: 211027

Since: 03-15-04
From: Canada, w00t!
LOL FAD

Since last post: 2 hours
Last activity: 2 hours
Posted on 11-10-04 06:47 AM Link | Quote
Yes, that's what I meant. I want the index to go from 50-99 instead of 0-49. (If I used that pointer trick, wouldn't it mess up a lot of the memory?)
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 1662/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 11-10-04 07:27 AM Link | Quote
if you aren't concerned with memory, then you can always allocate an array of size 100 and use it that way...otherwise, there is no way you can do that in C unless you use some layer of abstraction or a macro like...
#define SOME_ARRAY(a) some_array[a - 50];

int main()
{
int some_array[50];
printf("%d", SOME_ARRAY(50)); // same as printf("%d", some_array[50 - 50]);
return 0;
}
sloat

Level: 16

Posts: 13/85
EXP: 18044
For next: 2212

Since: 05-21-04
From: South Central Delaware

Since last post: 19 days
Last activity: 5 hours
Posted on 11-10-04 08:53 AM Link | Quote
couldn't you write a class and overload the [] operator?
or you could just stop beating around the bush and just use a 0 based index.
Dish

Spiny
Level: 38

Posts: 183/596
EXP: 355646
For next: 14801

Since: 03-15-04
From: Disch

Since last post: 18 days
Last activity: 18 days
Posted on 11-10-04 08:42 PM Link | Quote
Originally posted by HyperHacker
(If I used that pointer trick, wouldn't it mess up a lot of the memory?)


No.

The pointer trick simply takes the address (or pointer) of the array and subtracts 50 elements (not bytes) from it. So some_ptr will point to an area 50 elements before the area some_array points to. This allows you (and kind of forces you) to add an extra 50 to the index to access the same memory. The memory itself isn't changed though... not with any of that sample code I wrote.

but I'm with sloat. Just use a zero based index ;P
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - Apparently I don't know as much C as I thought... | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.012 seconds.