View Single Post
  #10   Spotlight this post!  
Unread 23-10-2005, 23:43
Joel J's Avatar
Joel J Joel J is offline
do you..
no team
 
Join Date: May 2001
Rookie Year: 2000
Location: San Jose, CA
Posts: 1,445
Joel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond reputeJoel J has a reputation beyond repute
Re: Typedef Struct as function input

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.
__________________
Joel Johnson

Division By Zero (229) Alumni, 2003-2007
RAGE (173) Alumni, 1999-2003