Log in

View Full Version : Typedef Struct as function input


Mike
23-10-2005, 17:18
Is there a way to have typedef struct's as input to a function? EG:


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 =)

sciguy125
23-10-2005, 17:33
Shouldn't a pointer work?

CyberWolf_22
23-10-2005, 17:43
Look at this white paper it uses structs as inputs to functions quite a few times http://www.chiefdelphi.com/forums/papers.php?s=&action=single&paperid=517

Mike
23-10-2005, 17:47
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...

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.

Mike
23-10-2005, 17:48
Look at this white paper it uses structs as inputs to functions quite a few times http://www.chiefdelphi.com/forums/papers.php?s=&action=single&paperid=517
Alright, I'll take a look in a bit. Gotta go eat now =D

foobert
23-10-2005, 17:57
Is there a way to have typedef struct's as input to a function? EG:


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);
}

Mike
23-10-2005, 18:53
OK, so I've solved some problem and caused some more.

Right now my code is...

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);

Ryan M.
23-10-2005, 19:34
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. :D


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.)

Typedef is used to give a current type a new name. Using typedef here... well, I don't know what it does. (typedef (http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/typedef.html), first Google result)
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.
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:


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);

Mike
23-10-2005, 21:01
Heck, I'm surprised it compiles that far. :D


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.)


Typedef is used to give a current type a new name. Using typedef here... well, I don't know what it does. (typedef (http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/typedef.html), first Google result)
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.
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:


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

Joel J
23-10-2005, 23:43
Here's what I learned to do..

First, I define the the structure:

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,

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.

Ryan M.
24-10-2005, 19:29
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.

BorisTheBlade
01-11-2005, 03:41
I've never used pointers before really, so could you show an example?


here is a very simple example of pointers for you Mike


#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.

BorisTheBlade
01-11-2005, 03:50
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.

MikeDubreuil
01-11-2005, 06:28
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.)