-
Structs as argument
Hi I want to pass in a struct as an argument in a function, in which this function pass the same struct to another function.
Code:
StructExample tyExample;
void function1(&tyExample);
Then in my function 1 which looks like this
Code:
void function1(StructExample* tyExample)
{
function2(tyExample);
}
Then my function 2 looks like this
Code:
void function2(StructExample* tyExample)
{
//some code to fill in tyExample
}
Is this the correct way to pass structs as arugment to different functions?
-
As long as you're aware that the names don't have to be the same (they can be if you want, but it can lead to confusion).
As to the question, I have no idea why you would expect passing a struct to a function to be different from passing a struct to a function. (EDIT: But I suppose I should say that you are correct: passing a struct to a function is not different from passing a struct to a function, so your code is fine.)
-
oh. I saw this example about char* pointers, hence the question about structs
Code:
int my_new(char **obj) {
*obj = malloc(somesize);
}
and then call this from your function like this:
Code:
char *obj;
my_new(&obj);
/* work on obj */
free(obj)
Hence was wondering if it applies to structs
-
I don't see how that is related to your original question at all. (Edit: Even if obj comes in as a parameter rather than a declaration, you are given a string and passing a pointer-to-string; in your original question both arguments are pointer-to-struct.)
-
oh i see i see. Thanks for the clarifications! =)