C code warnings that I can't figure out. Plz help!

Hey! It aint that hard!
Post Reply
Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 01/03/03, 3:52 pm

This is all pretty standard C stuff using file input and output. It's a small project that I have to do and that is the structure I had to use so don't blame me.

The basic program is:
- Read from input file to 2D array
- Output 2D array to output file
- Output output file to printer

Problem is it has 7 warnings in Borland Builder 4 which are all pretty much the same, but I can't figure out the stupid mistake that I'm making and when I try to run it, it has a runtime error that is associated with the first warning. Here they are (I have pointed to the throughout the code using comments ie. <i>// WARNING 5</i>):

1. [C++ Warning] proj.cpp(67): W8013 Possible use of 'file_in' before definition.
2. [C++ Warning] proj.cpp(76): W8013 Possible use of 'gender' before definition.
3. [C++ Warning] proj.cpp(76): W8013 Possible use of 'age' before definition.
4. [C++ Warning] proj.cpp(76): W8013 Possible use of 'lead' before definition.
5. [C++ Warning] proj.cpp(104): W8013 Possible use of 'file_out' before definition.
6. [C++ Warning] proj.cpp(207): W8013 Possible use of 'file_in' before definition.
7. [C++ Warning] proj.cpp(208): W8013 Possible use of 'printer' before definition.

Can anybody help???

Here is the <a href="http://www.boomspeed.com/gerrys_cs/code.c">C code - code.c (updated - all warnings fixed)</a> and the <a href="http://www.boomspeed.com/gerrys_cs/cust ... dat">input file - custsurvey.dat</a>. Both are just plain text files. The code won't post any more (I think it's gotten to big or something) so I decided to make them both downloads.

<font class=editedby>[ This Message was edited by: Gerry on 2003-01-04 11:19 ]</font>

<font class=editedby>[ This Message was edited by: Gerry on 2003-01-05 01:12 ]</font>

SOD
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5284
Joined: 12/06/01, 12:00 am
Location: here and there

Post by SOD » 01/03/03, 11:23 pm

Is it possible these need some kind of explicit definition, Or you have global definitions has the structure and code somehow made them explicit? I found a couple of spelling errors within the code structure.


<font class=editedby>[ This Message was edited by: SOD on 2003-01-04 07:34 ]</font>

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 01/04/03, 2:54 am

What is an explicit definition?

This is my declaration:
<i>FILE *file_in;</i>

and then I try to initialize the pointer with:
<i>open_stream(file_in, INPUT_FILE, "r");</i>

which is where the first warning occurs, but I'm not sure what you mean when you say "explicit definition". I have included all of the code except for the three standard C libraries <i>stdio.h</i>, <i>conio.h</i> and <i>string.h</i> so apart from those, nothing else external could be having an effect on the code and I very much doubt it is any of the standard libraries as there are seven different warnings. Things like this usually crop up when I make an <s>stupid mistake</s> error.

Heh, I see what you mean by spelling errors, however I have spelt everything wrong the same way at every occurrence so it really didn't make any difference to the compiler, however I've fixed it now but it still doesn't work. I've also added a "flag check" to make sure the program doesn't continue if a file can't be opened.

Also in case anybody wants to test it out, the input file has the following structure:

Column #1: Gender M or F
Column #2: Age 1 - 123
Column #1: Lead 1 - 6

ex.

<pre>F 26 5
F 64 4
F 29 2
M 12 3
M 40 1
F 38 4
M 19 5
F 66 5
F 14 5
M 15 5
F 67 1</pre>
<i>- Edit I have posted the input file above so you don't really need to worry about it now, just make sure to set the correct location for the file in the #define at the top of the C code.</i>

Again I'd appreciate any help anybody can give me and thanks for your time so far SOD.

_________________
<small><a href="http://www.free2code.net/top50/in.php?id=17" target="_blank">Vote for shellcity.net on Free2Code.net!</a></small>

<font class=editedby>[ This Message was edited by: Gerry on 2003-01-04 11:24 ]</font>

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 01/04/03, 4:55 pm

OK well my runtime error actually wasn't being caused by the warnings which I have now fixed (warnings 2, 3 and 4 were due to my stupidity, forgetting to add <i>&</i> operator to the front of my variables when using <i>fscanf()</i> and the others were just the compiler paranoia which didn't like my hanging pointers, so initializing them to NULL at declaration time fixed that [thanks SOD])

I have figured out what the problem is now, but I still can't figure out how to fix it.

I have included the snippet below that contains the run-time error. Basically <i>main()</i> is supposed to declare a file pointer (<i>file_in</i>) then send it to <i>open_stream()</i>. <i>open_stream()</i> is than supposed to declare a local file pointer (<i>fp</i>) to the same address and change pointers value so that it points to the output file. Then once the program returns to <i>main()</i>, <i>file_in</i> should also contain the new value which points to the input file. But it doesn't, <i>fp</i>'s value gets changed but <i>file_in</i>'s value doesn't.

Can anybody spot the mistake I'm making cause I can't. If I fix this then that's everything fixed *crosses fingers*.

<!-- BBCode Start --><TABLE BORDER=0 ALIGN=CENTER WIDTH=85%><TR><TD><font class=postbody>Code:</font><HR width=100% color=#333333 SIZE=1></TD></TR><TR><TD><FONT class=code><PRE>#define INPUT_FILE "d:sp3workcustsurvey.dat"

void open_stream(FILE *fp, char address[], char operation[])
{
fp = fopen(address, operation);
if(fp==NULL)
{
printf("%s", OPEN_ERROR_MSG);
flag=0;
}
else
printf("WERE IN!!!");
}

void main(void)
{
FILE *file_in = NULL;
open_stream(file_in, INPUT_FILE, "r"); </PRE></FONT></TD></TR><TR><TD><HR width=100% color=#333333 SIZE=1></TD></TR></TABLE><!-- BBCode End -->
I answer rhetorical questions for my own enjoyment.

Gerry
BIG GIANT HEAD I Get Free Beer
BIG GIANT HEAD I Get Free Beer
Posts: 5727
Joined: 12/04/01, 12:00 am
Location: Perth, Western Australia
Contact:

Post by Gerry » 01/05/03, 2:07 am

Ehh... I should of been passing a pointer to a pointer not just a pointer. 350 lines of code, mostly cut down by defines and macros (god I love them), and I'm finally finished. Oh crap that's right I gotta go back and update the documentation. *collapses in a heap*
I answer rhetorical questions for my own enjoyment.

Post Reply