|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
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);
|
|
#2
|
|||
|
|||
|
Re: Typedef Struct as function input
Shouldn't a pointer work?
|
|
#3
|
|||||
|
|||||
|
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
|
|||||
|
|||||
|
Re: Typedef Struct as function input
Quote:
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);
|
|
#5
|
|||||
|
|||||
|
Re: Typedef Struct as function input
Quote:
|
|
#6
|
|||
|
|||
|
Re: Typedef Struct as function input
Quote:
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
|
|||||
|
|||||
|
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);
EDIT: The error is on Func(object_example); Last edited by Mike : 23-10-2005 at 18:55. |
|
#8
|
||||
|
||||
|
Re: Typedef Struct as function input
Quote:
![]() 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);
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
|
|||||
|
|||||
|
Re: Typedef Struct as function input
Quote:
|
|
#10
|
||||
|
||||
|
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;
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;
}
|
|
#11
|
||||
|
||||
|
Re: Typedef Struct as function input
Quote:
|
|
#12
|
||||
|
||||
|
Re: Typedef Struct as function input
Quote:
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;
}
|
|
#13
|
||||
|
||||
|
Re: Typedef Struct as function input
Quote:
|
|
#14
|
|||||
|
|||||
|
Re: Typedef Struct as function input
Quote:
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.) Last edited by MikeDubreuil : 01-11-2005 at 06:31. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
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 |