What is the advantage of usinga pointer to a structure as a parameter to a funciton, instead of the structure itself?
I think it makes it more efficient because the structure is not copied. Am I right?
What is the advantage of usinga pointer to a structure as a parameter to a funciton, instead of the structure itself?
I think it makes it more efficient because the structure is not copied. Am I right?
If you want to change the members of a struct in a function, you have to pass the struct's address.
Can you elaborate MacGyver?
Output:Code:#include <stdio.h> struct bleh { int x; }; void breakstruct(struct bleh); int main(void) { struct bleh somestruct; somestruct.x = 5; breakstruct(somestruct); printf("somestruct.x = %d\n",somestruct.x); return 0; } void breakstruct(struct bleh strct) { strct.x = 100; }
The function can't change the value of x of the struct passed to it. If a pointer to that struct was passed, then it could alter it.Code:somestruct.x = 5
So passing a structure as a parameter is not allowed?
No, it's allowed, but it's like passing an int to a function. If you want to change the integer that you pass inside the function and make it reflect the changes inside the function that called it (like main()), you can't do that. You have to pass the address of the int.
If you know about the difference between passing an int by value and passing it by reference, then it's the same thing with structs. Don't overcomplicate it just because it's a struct.
Thanks MacGyver!!
By having a function take a pointer to a structure, such as to load a BMP file and fill in the info header data while reading from a file. It makes it more convenient and easier to do. Instead of the dot to access an element, you'd have the "->" instead if a pointer is used. My LoadFile function takes such a pointer.
High elevation is the best elevation. The higher, the better the view!
My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express
And if you ever learn C++ you will also learn passing by reference using '&' which is handy when passing very large objects to a function as much less overhead is made. But this is a C board so ignore that
Double Helix STL