zoukankan      html  css  js  c++  java
  • c语言中返回结构体的函数(结构体可以被赋值,类型相同的结构体可以相互赋值)

    1、

    #include <stdio.h>
    
    struct xyz{
        int x;
        long y;
        double z;
    };
    
    struct xyz fun(int a, long b, double c)  //函数的返回类型为struct xyz型 
    {
        struct xyz tmp;  //声明结构体对象tmp 
        
        tmp.x = a;  // 为结构体对象的成员赋值 
        tmp.y = b;
        tmp.z = c;
        
        return tmp;  // 返回结构体对象tmp 
    }
    
    int main(void)
    {
        struct xyz result = {10, 30, 3.8};  // 声明结构体对象result 
        
        result = fun(200, 400, 88.8);  //相同类型的结构体对象之间可以相互赋值,result结构体对象和fun函数返回的结构体对象都是 struct xyz型,因此可以相互赋值。 
        
        printf("result.x:  %d
    ", result.x);
        printf("result.y:  %ld
    ", result.y);
        printf("result.z:  %.2f
    ", result.z);
        
        return 0;
    }

  • 相关阅读:
    HDU 5319 Painter
    HDU 5328 Problem Killer
    HDU 5327 Olympiad
    HDU 5339 Untitled
    HDU 5335 Walk Out
    HDU 5317 RGCDQ
    HDU 5326 Work
    CF GYM 100703A Tea-drinking
    CF GYM 100703B Energy Saving
    CF GYM 100703F Game of words
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14852339.html
Copyright © 2011-2022 走看看