大话C语言:第41篇 结构体与函数的关系

1 结构体作为函数的参数

结构体作为函数的参数,存在两种传递方式:

  • **值传递:**将结构体的值拷贝一份传递给函数,函数内部对该结构体的修改不会影响到原来的结构体变量。

代码示例:

cpp 复制代码
#include <stdio.h>
#include <string.h>

// 结构体类型的定义
struct Student 
{  
    char name[20];  
    int age;  
    float score;  
};  

// 函数定义
void ShowStudent(struct Student stu) 
{
    strcpy(stu.name, "jack");
    stu.age = 22;
    stu.score = 98.5f;
    printf("ShowStudent函数里,该学生:%s, %d, %.1f\n", stu.name, stu.age, stu.score);
}

int main() 
{
    // 定义结构体变量
    struct Student stu = {"peter", 20, 90.5f};
    
    // 调用函数,值传递
    ShowStudent(stu);
    
    // 打印成员变量
    printf("main函数里,该学生:%s, %d, %.1f\n", stu.name, stu.age, stu.score);

    return 0;
}
  • **地址传递:**将结构体的地址传递给函数,函数内部可以通过该地址来访问原变量,并对其进行修改。

代码示例:

cpp 复制代码
#include <stdio.h>
#include <string.h>

// 结构体类型的定义
struct Student 
{  
    char name[20];  
    int age;  
    float score;  
};  

// 函数定义
void ShowStudent(struct Student *stu) 
{
    strcpy(stu->name, "jack");
    stu->age = 22;
    stu->score = 98.5f;
    printf("ShowStudent函数里,该学生:%s, %d, %.1f\n", stu->name, stu->age, stu->score);
}

int main() 
{
    // 定义结构体变量
    struct Student stu = {"peter", 20, 90.5f};
    // 打印成员变量
    printf("main函数里,ShowStudent函数调用前,该学生:%s, %d, %.1f\n", stu.name, stu.age, stu.score);
    
    // 调用函数,值传递
    ShowStudent(&stu);
    
    // 打印成员变量
    printf("main函数里,ShowStudent函数调用后,该学生:%s, %d, %.1f\n", stu.name, stu.age, stu.score);

    return 0;
}

2 结构体作为函数的返回值

结构体作为函数的返回值,存在两种返回方式:

  • 直接返回结构体:如果结构体较小,内容简单,可以直接将其作为函数的返回值。这种方式简单直观,不需要额外的内存管理操作。

代码示例:

cpp 复制代码
#include <stdio.h>

// 结构体类型的定义
struct Student 
{  
    char name[20];  
    int age;  
    float score;  
};

// 直接返回结构体
struct Student GenerateStudent()
{
    struct Student stu = {"peter", 20, 90.5f};
    
    return stu;
}


int main()
{
    struct Student stu = GenerateStudent();
    
    printf("该学生:%s, %d, %.1f\n", stu.name, stu.age, stu.score);
    
    return 0;
}

注意,当结构体较大时,直接返回可能会导致性能问题,因为函数返回时会复制整个结构体。

  • 返回结构体指针:当结构体较大或者需要在函数外部修改结构体内容时,通常使用返回结构体指针的方式。这样可以避免大量数据的复制,提高性能。

代码示例:

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 结构体类型的定义
struct Student 
{  
    char name[20];  
    int age;  
    float score;  
}; 
  
struct Student* GenerateStudent() 
{  
    struct Student *stu = (struct Student*)malloc(sizeof(struct Student));  
    if (stu != NULL) {  
        strcpy(stu->name, "jack");
        stu->age = 22;
        stu->score = 98.5f;
    }
    
    return stu; // 返回结构体指针  
}  
  
int main()
{  
    struct Student *stu = GenerateStudent();  
    
    if (stu != NULL) 
    {  
        printf("该学生:%s, %d, %.1f\n", stu->name, stu->age, stu->score);
        
        // 使用完毕后释放内存   
        free(stu); 
        stu = NULL;
    }  
    
    return 0;  
}

注意,使用结构体指针需要谨慎处理内存管理问题,避免内存泄漏或野指针等问题。

相关推荐
森焱森5 小时前
无人机三轴稳定控制(2)____根据目标俯仰角,实现俯仰稳定化控制,计算出升降舵输出
c语言·单片机·算法·架构·无人机
小林C语言8 小时前
C语言 | 判断是否为回文数
c语言
PMEcho8 小时前
墨刀监听变量实战:1个案例搞定高保真交互原型(附教程)
axure·变量·函数·交互设计·墨刀·原型设计·条件判断·高保真原型·监听·高级交互·高保真交互原型
myloveasuka10 小时前
信号操作集函数
linux·运维·服务器·c语言·c++·vscode
Mr_Xuhhh11 小时前
网络基础(1)
c语言·开发语言·网络·c++·qt·算法
双叶83610 天前
(C语言)Map数组的实现(数据结构)(链表)(指针)
c语言·数据结构·c++·算法·链表·哈希算法
不会kao代码的小白10 天前
C指针总结复习(结合deepseek)
c语言
XiaoCCCcCCccCcccC10 天前
C语言数组介绍 -- 一维数组和二维数组的创建、初始化、下标、遍历、存储,C99 变长数组
c语言·数据结构·算法
岁忧11 天前
第十六届蓝桥杯C/C++程序设计研究生组国赛 国二
c语言·c++·算法·蓝桥杯
一ge科研小菜鸡11 天前
编程语言的设计之道:从底层控制到表达自由
java·c语言·c++·python