I'm always so wary about posting my code comments as I do this for work but always screw up when not in front of a compiler! Also I've been coding in C++ for the last couple of years and my C isn't strictly ANSI compliant anymore ... end disclaimer there :-D here goes.
1) A function can only "
return" a single item. However that item can be a composite such as "struct", a pointer to other information, a reference to another item that does exist or a value.
This is where I might screw up the C vs C++ syntax btw. You don't need to pass in pointers to a function to change their value instead you can pass by reference.
for example:
// you could package all of your return values up like this.
typedef struct compositeItem{
float theHamster;
int downTheStream;
int unrelatedGubbins;
};
//and then your function would return like this.
compositeItem someDumbFunc()
{
compositeItem bodge;
//do funky things that fill compositeItem here
return bodge;
}That would be one
really bad way of doing it :-D instead you can pass by pointer or by reference.
void SonOfDumbFuncPointer( compositeItem *pCompItem, int *pVal )
{
// dereference the pointer to access the values location
(*pCompItem).theHamster = 0.0f;
(*pCompItem).downTheStream = *pVal;
(*pCompItem).unrelatedGubbins = *pVal;
++(*pVal);
}
void SonOfDumbFuncRef( compositeItem &rCompItem, int &rVal )
{
rCompitem.theHamster = 0.0f;
rCompitem.downTheStream = rVal;
rCompitem.unrelatedGubbins = rVal;
++rVal;
}2) Arrays and pointers are, well ok they're not something to be shied away from in C or there's no point learning the language. You're correct in that the elements of an arrays can only be of one "type". That type can be made of either the language defined base types or your own.
See here for a reasonable explanation. Actually I could have skipped all of the above if I'd thought of this sooner :idea:
Passing Parameters 3) You might pass a structure to erm... I dunno, serialise it for lobbing over a network? No, I dunno. If I pass a structure I usually pass by reference or pointer and make it "const" if I don't want it to be modified. I'm sure that there's some really awesome and intelligent reason for passing a structure by value, that... y'know, someone that knows and will seem obvious once they mention it :-D
Hope that didn't confuse things more.
Andy