Struct variables are used as function parameters and return values.

Source: Internet
Author: User
Struct variables are used as function parameters and return values.

Prepare to learn opencv, but the syntax of the first knowledge point is not understood:

 
Typedef struct cvpoint {int X;/* x coordinate, usually based on 0 */INT y;/* Y coordinate, usually based on 0 */} cvpoint;
 
/* Constructor */inline cvpoint (int x, int y );
 
Note that the cvpoint struct is C in upper case, while the cvpoint () constructor is C in lower case. If the constructor is the same, it cannot be compiled.
 
So I searched and found out,The original struct can be used as a function parameter or as a function return value..
 
I used to use struct pointers all the time. I don't know what is the difference between directly using struct variables and using struct pointer variables?
However, in opencv, many functions directly use struct variables. Maybe this operation is faster ??
 
Google has been using this for a long time and finds the answer:
 
In this case, the function uses a copy of the struct to call the value passing belonging to the function. Operations in the function do not change the value of the original struct, but will sacrifice some speed.
 
 
 
If you use a pointer to pass a struct variable, it is an address-based call that directly operates the struct, so the operations in the function are directly reflected on the struct. In opencv, the first is that the structure is relatively small, and the memory overhead is relatively small; the second is to avoid changing the structure itself. For more information, see.
 
The following is an example of C/C ++. The compilation test is normal.
/*************************************** * Struct variables can be directly used as function parameters, it can also be used as the return value of a function. **************************************** * // Filename: struct. C # include <stdio. h> // define a struct typedef struct point {int X; int y;} Point;
 
// Use a struct variable as the void display (point) {printf ("X is % d \ n", point. x); printf ("Y is % d \ n", point. y);/********* if C ++ is used, the following *************** STD :: cout <"X is" <point. x <STD: Endl; STD: cout <"Y is" <point. Y <STD: Endl; **************************************/}
// Use struct variables as the return value of the function. Point setpoint (int x, int y) {point; point. x = x; point. Y = y; return point ;}
 
// Main function int main (INT atgc, char * argv []) {point; point = setpoint (2, 3); display (point); Return 0 ;}
 
Running resultXIs 2y is 3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.