Go to Post On day two of build season, I made the mistake of saying "We have six weeks, what could possibly go wrong?" - Zach Herbst [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 23-10-2005, 17:18
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Typedef Struct as function input

Is there a way to have typedef struct's as input to a function? EG:

Code:
void Function(typedef struct data)
{
printf("%s \n", data.something);
}

typedef struct{
int blah;
} typedef_example;

typedef_example data;
data.blah = 1;

Function(data);
Thanks =)
__________________
http://www.mikesorrenti.com/
  #2   Spotlight this post!  
Unread 23-10-2005, 17:33
sciguy125 sciguy125 is offline
Electrical Engineer
AKA: Phil Baltar
FRC #1351
Team Role: College Student
 
Join Date: Jan 2005
Rookie Year: 2004
Location: Sunnyvale, CA
Posts: 519
sciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond reputesciguy125 has a reputation beyond repute
Send a message via AIM to sciguy125 Send a message via MSN to sciguy125 Send a message via Yahoo to sciguy125
Re: Typedef Struct as function input

Shouldn't a pointer work?
__________________

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GE/S/P a-- e y-- r-- s:++ d+ h! X+++
t++ C+ P+ L++ E W++ w M-- V? PS+ PE+
5- R-- tv+ b+ DI+++ D- G
------END GEEK CODE BLOCK------
  #3   Spotlight this post!  
Unread 23-10-2005, 17:43
CyberWolf_22's Avatar
CyberWolf_22 CyberWolf_22 is offline
Programming and Electrical Mentor
AKA: Allen Gregory
FRC #2587 (Afrobots)
Team Role: Mentor
 
Join Date: Jan 2003
Rookie Year: 2003
Location: Houston, Texas
Posts: 227
CyberWolf_22 is just really niceCyberWolf_22 is just really niceCyberWolf_22 is just really niceCyberWolf_22 is just really nice
Re: Typedef Struct as function input

Look at this white paper it uses structs as inputs to functions quite a few times http://www.chiefdelphi.com/forums/pa...le&paperid=517
__________________
  #4   Spotlight this post!  
Unread 23-10-2005, 17:47
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Typedef Struct as function input

Quote:
Originally Posted by sciguy125
Shouldn't a pointer work?
I've never used pointers before really, so could you show an example?

The way I think it's supposed to work (but isn't) would be to have something like...
Code:
void Function(*data);

void Function(*data)
{
printf("%s \n", data.something);
}

typedef struct{
int blah;
} typedef_example;

typedef_example data;
data.blah = 1;

Function(&data);
But it's getting a syntax error on the function prototype.
__________________
http://www.mikesorrenti.com/
  #5   Spotlight this post!  
Unread 23-10-2005, 17:48
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Typedef Struct as function input

Quote:
Originally Posted by CyberWolf_22
Look at this white paper it uses structs as inputs to functions quite a few times http://www.chiefdelphi.com/forums/pa...le&paperid=517
Alright, I'll take a look in a bit. Gotta go eat now =D
__________________
http://www.mikesorrenti.com/
  #6   Spotlight this post!  
Unread 23-10-2005, 17:57
foobert foobert is offline
Registered User
no team
 
Join Date: May 2005
Location: oakland, ca
Posts: 87
foobert is a jewel in the roughfoobert is a jewel in the roughfoobert is a jewel in the rough
Re: Typedef Struct as function input

Quote:
Originally Posted by Mike
Is there a way to have typedef struct's as input to a function? EG:

Code:
void Function(typedef struct data)
{
printf("%s \n", data.something);
}

typedef struct{
int blah;
} typedef_example;

typedef_example data;
data.blah = 1;

Function(data);
Thanks =)

seems to me you want...

typedef struct{
int blah;
} typedef_example;


void Function (typedef_example data) {
printf("%s \n", data.blah);
}

void main () {
typedef_example data;
data.blah = 1;
Function (data);
}
  #7   Spotlight this post!  
Unread 23-10-2005, 18:53
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Typedef Struct as function input

OK, so I've solved some problem and caused some more.

Right now my code is...
Code:
void Func(struct typedef_example *variable);

void Func(struct typedef_example *variable)
{
...
}

typedef struct
{
  int x;
} typedef_example;
typedef_example object_example;

object_example.x = 2;

Func(object_example);
I'm getting Error [1146] type mismatch in argument 1 when I try to compile. Any ideas?

EDIT: The error is on Func(object_example);
__________________
http://www.mikesorrenti.com/

Last edited by Mike : 23-10-2005 at 18:55.
  #8   Spotlight this post!  
Unread 23-10-2005, 19:34
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Typedef Struct as function input

Quote:
Originally Posted by Mike
OK, so I've solved some problem and caused some more.

Right now my code is...
I'm getting Error [1146] type mismatch in argument 1 when I try to compile. Any ideas?

EDIT: The error is on Func(object_example);
Heck, I'm surprised it compiles that far.

Code:
void Func(struct typedef_example *variable);

void Func(struct typedef_example *variable)
{
...
}

typedef struct name_here
{
  int x;
} typedef_example;
typedef_example object_example;

object_example.x = 2;

Func(object_example);
The three things in red illustrate the error. (I think.)
  1. Typedef is used to give a current type a new name. Using typedef here... well, I don't know what it does. (typedef, first Google result)
  2. The declaration of object_example is made using typedef_example, which is actually an instance of the struct. The blue shows you where you instanciated it. (You can do the same thing with C++ classes, which is why you have to have that ending semicolon.) To actually do this, you should give the struct a name (where the yellow is), then use that.
  3. And the one which is definetely causing the error... you forgot the ampersand. (Gets the address of the variable, which is what a pointer holds, vs. the way you have it now which is the actual struct.)

That said, it is possible that C supports this odd annonymous struct typedefed thing, but the way I'd do it would be:

Code:
void Func(struct typedef_example *variable);

struct typedef_example
{
  int x;
};

void Func(struct typedef_example *variable)
{
...
}

struct typedef_example object_example;
object_example.x = 2;
Func(&object_example);
__________________

  #9   Spotlight this post!  
Unread 23-10-2005, 21:01
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Re: Typedef Struct as function input

Quote:
Originally Posted by Ryan M.
Heck, I'm surprised it compiles that far.

Code:
void Func(struct typedef_example *variable);

void Func(struct typedef_example *variable)
{
...
}

typedef struct name_here
{
  int x;
} typedef_example;
typedef_example object_example;

object_example.x = 2;

Func(object_example);
The three things in red illustrate the error. (I think.)
  1. Typedef is used to give a current type a new name. Using typedef here... well, I don't know what it does. (typedef, first Google result)
  2. The declaration of object_example is made using typedef_example, which is actually an instance of the struct. The blue shows you where you instanciated it. (You can do the same thing with C++ classes, which is why you have to have that ending semicolon.) To actually do this, you should give the struct a name (where the yellow is), then use that.
  3. And the one which is definetely causing the error... you forgot the ampersand. (Gets the address of the variable, which is what a pointer holds, vs. the way you have it now which is the actual struct.)
That said, it is possible that C supports this odd annonymous struct typedefed thing, but the way I'd do it would be:

Code:
void Func(struct typedef_example *variable);

struct typedef_example
{
  int x;
};

void Func(struct typedef_example *variable)
{
...
}

struct typedef_example object_example;
object_example.x = 2;
Func(&object_example);
Thanks a ton, it works now =D
__________________
http://www.mikesorrenti.com/
  #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
  #11   Spotlight this post!  
Unread 24-10-2005, 19:29
Ryan M. Ryan M. is offline
Programming User
FRC #1317 (Digital Fusion)
Team Role: Programmer
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Ohio
Posts: 1,508
Ryan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud ofRyan M. has much to be proud of
Re: Typedef Struct as function input

Quote:
Originally Posted by Joel J.
Code:
typedef struct
{
	int x;
} ExampleType, *pExampleType;
Well... I've never ever ever heard of doing it this way, but apparently it's valid. Hm. You learn something new every day.
__________________

  #12   Spotlight this post!  
Unread 01-11-2005, 03:41
BorisTheBlade's Avatar
BorisTheBlade BorisTheBlade is offline
lead programmer 2004
AKA: Dane
FRC #1351 (TKO)
Team Role: Mentor
 
Join Date: Oct 2005
Rookie Year: 2004
Location: San Jose, Ca
Posts: 23
BorisTheBlade has a spectacular aura aboutBorisTheBlade has a spectacular aura aboutBorisTheBlade has a spectacular aura about
Send a message via AIM to BorisTheBlade
Re: Typedef Struct as function input

Quote:
Originally Posted by Mike
I've never used pointers before really, so could you show an example?
here is a very simple example of pointers for you Mike

Code:
#include <stdio.h>

int modifyVariables(int *pB, char *pC, float *pD);

int main()
{
int a;
int b;
char c;
float d;

a = modifyVariables(&b, &c, &d);

printf("%d\n", a); //prints 7
printf("%d\n", b); //prints 6
printf("%c\n", c); //prints g
printf("%2.1f\n", d); //prints 22.1
return 0;
}

int modifyVariables(int *pB, char *pC, float *pD)
{
*pB = 6; //sets int b in main function to 6
*pC = 'g'; //sets char c in main function to g
*pD = 22.1; //sets float d in main function to 22.1
return 7;
}
you can only return one variable from a function how ever by sending pointers to variables you can modify more than one variable without returning the values in a return statement.
__________________
Team 1351 TKO Robotics (click for our website)
2007 sacramento regional Johnson & Johnson Sportsmanship Award winners
2007 CAL Games Finalists
  #13   Spotlight this post!  
Unread 01-11-2005, 03:50
BorisTheBlade's Avatar
BorisTheBlade BorisTheBlade is offline
lead programmer 2004
AKA: Dane
FRC #1351 (TKO)
Team Role: Mentor
 
Join Date: Oct 2005
Rookie Year: 2004
Location: San Jose, Ca
Posts: 23
BorisTheBlade has a spectacular aura aboutBorisTheBlade has a spectacular aura aboutBorisTheBlade has a spectacular aura about
Send a message via AIM to BorisTheBlade
Re: Typedef Struct as function input

Quote:
Originally Posted by Mike
Is there a way to have typedef struct's as input to a function?
just curious as to why you would want to do that? I don't see why you would ever want to do that. I mean the whole point of a structure is you can access it from anywhere or create a new structure in any function in your program.
__________________
Team 1351 TKO Robotics (click for our website)
2007 sacramento regional Johnson & Johnson Sportsmanship Award winners
2007 CAL Games Finalists
  #14   Spotlight this post!  
Unread 01-11-2005, 06:28
MikeDubreuil's Avatar
MikeDubreuil MikeDubreuil is offline
Carpe diem
FRC #0125 (Nu-Trons)
Team Role: Engineer
 
Join Date: Jan 2003
Rookie Year: 1999
Location: Boston, MA
Posts: 967
MikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond reputeMikeDubreuil has a reputation beyond repute
Send a message via AIM to MikeDubreuil
Re: Typedef Struct as function input

Quote:
Originally Posted by BorisTheBlade
just curious as to why you would want to do that? I don't see why you would ever want to do that. I mean the whole point of a structure is you can access it from anywhere or create a new structure in any function in your program.
The point of a structure is to keep a common set of variables grouped together. For instance you could create a structure for a robot's arm, with a variable for the motor and the sensor monitoring the angle.

One concept of "good programming technique" is to keep the variable's scope so that only functions that require access to it are able to. Structures are variables and this rule applies. In general you want to try to keep as few globals as possible. Instead, declare a variable static in a function; that will keep the value of the variable after it is removed from the stack (gone to a different function.)
__________________
"FIRST is like bling bling for the brain." - Woodie Flowers

Last edited by MikeDubreuil : 01-11-2005 at 06:31.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
TTL port to a serial port on a demo board ImmortalAres Programming 16 09-07-2005 23:44
Accelerometer code ImmortalAres Programming 28 04-06-2005 01:02
Auton + Functions ten3brousone Programming 0 27-02-2005 20:11
RoboEmu2(code simulator)--now with C! rbayer Programming 23 17-02-2005 09:17
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 04:36.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi