C: Functions and scope of variables

krishnaz

n00b
Joined
Jul 6, 2007
Messages
18
I want to create a void function that can input strings (read from a file) into a series of character arrrays, which I have defined within a struct. Then I need to be able to use the struct char array within the block I have called the function in, and then have that struct char arrays destroyed once it leaves the block. I need them destroyed so that I can use the struct char arrays for different strings.

Is this possible in c?

I'm guessing that this is possible if I use an inline function. If so, am I right in saying that I can use variables defined in main within the inline function?

Sorry if the above makes no sense what so ever!
 
Sounds like you want a function that reads from a file, and builds the structure. So, you'd pass the file and a pointer to the structure to the function:

Code:
void ReadStuff( FILE *pFile, struct YourStructureType *pData );

Why do you want a void function? This make it either difficult or less obvious for the function to report errors or success back to its caller.

Your goal of having the structures destroy themselves when you leave the block can't be achieved in C; you want C++ for that.
 
Thanks for the reply!

Your right about the void. I was thinking I just needed a void one because everything was going to be done to just the struct. But you're right it would be a good idea to at least return if the function was carried out sucessfully.

I think I'll get around the not being able to destroy thing by creating a function to wipe the struct fields once I am done with them.

Another thing, I will be using an array of structs (I'm creating a simple database incase you have not guessed!). How will I get the function to go to the next struct? I'm guessing it's not as simple as pData++?
 
Another thing, I will be using an array of structs (I'm creating a simple database incase you have not guessed!). How will I get the function to go to the next struct? I'm guessing it's not as simple as pData++?


if you create a pointer of the struct type and initialize it to the front of your array, when you pointer++, the pointer will be modified to be [original address + sizeof(your structure)]
 
Say you're going to store a name and an age. I'd make a structure to store that payload:

Code:
struct Data
{
   char szName[80];	// a name can't be more than 79 characters
   unsigned short nAge;	// age
};

Then, make another structure with the array.

Code:
struct ArrayOfData
{
   unsigned short nItems;
   struct Data[100];	// can't have more than 100 items
};

This always allocates 100 items, and you'll have to remember to initialize nItems to zero when you create an ArrayOfData. But once that's done, you can pass the address of an ArrayOfData to whatever function needs it. That function can do whatever it wants to the values.

Maybe you'll want to make the items dynamically sized, or use an array or something ... but hopefully that gets you started.
 
Back
Top