Here's what I learned to do..
First, I define the the structure:
Code:
typedef struct
{
int x;
} ExampleType, *pExampleType;
You'll notice there are two types associated with that struct, a regular one (ExampleType), and a pointer type (pExampleType). Whereever I'd need to access the struct through a pointer, I'd use pExampleType in the declaration, and where I'd access the declared variable directly, I just use the regular definition (ExampleType).
For example,
Code:
void ShowStructData (pExampleType ExampleTypeAsPointer) // Since the function expects
// an address, and not actual
// data, use pointer type, so you can
// _point_ to the memory address sent
// to the function, and retrieve the
// data in it, or modify the data in it.
{
printf("%s \n", (ExampleTypeAsPointer->x)); // Prints: 326;
// Since ExampleTypeAsPointer refers to the address of
// ExampleTypeAsVariable, changes to ExampleTypeAsPointer
// are maintained once you leave the function. That is,
// you are changing ExampleTypeAsVariable to 100 in the
// next line of code:
ExampleTypeAsPointer->x = 100;
// Normally, you send a function a value, and that value would be copied
// to a memory location different from the one in which it is store for
// regular use. All changes made to the variable would be applied to the
// temporary memory location, as the function would not know, or be able
// to access, the location of the original variable. When the function ends
// the temporary memory location is freed, and some other part of the code
// can use it, and the memory location for the original variable is untouched.
// This is not true of pointers, because they point to the original variable,
// not a copy of it. Since you said you haven't used pointers much, this is
// something you should know as you go to use them.
}
void main()
{
ExampleType ExampleTypeAsVariable;
ExampleTypeAsVariable->x = 326;
printf("%s \n", (ExampleTypeAsVariable.x)); // Prints: 326
ShowStructData(&ExampleTypeAsVariable); // the "&" passes the memory address of
// ExampleTypeAsVariable to the
// ShowStructData function, and not the
// actual data in ExampleTypeAsVariable.
printf("%s \n", (ExampleTypeAsVariable.x)); // Prints 100
return;
}
I ended up commenting the code. Nothing left to say.