A variable you create, remains alive until the corresponding closing curly bracket }
of the opening curly bracket {
before it.
Each pair of opening and closing curly brackets and the contents within { ... }
are referred as code blocks, and with this definition, we can say that a variable remains alive within the code block it is declared.
You are declaring one roomsInfo[6]
array inside the function parser
, and it looks like you are hoping to be able to access it directly without doing anything from another function: drawingRooms
Things won't work out like that.
One another thing, a C compiler shouldn't be allowing you to call a function that asks for an argument, without an argument. So you shouldn't be able to make the call drawingRooms( );
at all, for void drawingRooms( room roomInfo[6] ) { ... }
defined like that. In case it does allow, then it probably regards room roomInfo[6]
as an un-initialized variable, like this:
void drawingRooms( )
{
room roomInfo[6];
...
}
Anyway, what you should be doing is to feed the drawingRooms
with an appropriate argument, which would be the address, the pointer to the first element of the array of 6 room
structures; if you have declared its function like that.
To resolve this specific issue, you have two options, I'll start with the easy one:
1) Making roomInfo[6]
a file-scope variable
By making the roomInfo[6]
a global (file-scope) variable, the variable will live through the whole life-time of the programme, thus will be accessible from everywhere in the code. To do this:
- delete the line
room roomInfo[6];
inside theparser
function - remove the argument
room roomInfo[6]
from the functiondrawingRooms
- declare
room roomInfo[6];
above everything else, above themain
function
Like this:
#define STARTING_POINT 5
//somewhere above the roomInfo declaration, somewhere here
//there has to be the typedef for the room structure,
//either through the inclusion of a header file
//or the typedef struct {...} room; itself
room roomInfo[6];
int main( ... )
{
...
...
2) Pushing the pointer to the drawingRooms
To do this, you'll have to first allocate memory for the array of room
structures yourself, so that it won't get killed automatically when the parser
function ends; then return the pointer to the first element with the parser
function, use that returned address while calling the drawingRooms
function:
- replace the line
room roomInfo[6];
withroom * roomInfo;
in theparser
function - allocate 6
room
structure-worth memory for your rooms
Like this:
roomInfo = calloc( 6, sizeof * roomInfo );
- make sure you have included either
<stdlib.h>
or<malloc.h>
, for thecalloc
call - add this line at the end of
parser
function:return roomInfo;
- change the return value of
parser
function fromvoid
intoroom *
- use this return value inside the
main
function while calling thedrawingRooms
function
Like this:
//either like this:
...
input( argc, argv );
drawingRooms( parser( argc, argv ) );
...
//or like this (allows you to free it inside main):
...
room * roomInfo;
input( argc, argv );
roomInfo = parser( argc, argv );
drawingRooms( roomInfo );
...
- if your programme is to run further, make sure you
free
the allocated memory.