c語言中返回結構體的函數。(相同類型的結構體可以相互賦值。)。
1、
#include <stdio.h> struct xyz{ int x; long y; double z; }; struct xyz fun(int a, long b, double c) { struct xyz tmp; // 創建臨時結構體 tmp.x = a; tmp.y = b; tmp.z = c; return tmp; } int main(void) { struct xyz result = {100, 200, 300}; result = fun(1111,33333,8888.34); // 函數中的tmp結構體和result結構體類型均為 struct xyz型,因此可以相關賦值。 printf("result.x = %d\n", result.x); printf("result.y = %ld\n", result.y); printf("result.z = %.1f\n", result.z); return 0; }