大话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;  
}

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

相关推荐
三体世界1 天前
测试用例全解析:从入门到精通(1)
linux·c语言·c++·python·功能测试·测试用例·测试覆盖率
程序员东岸1 天前
C语言入门指南:字符函数和字符串函数
c语言·笔记·学习·程序人生·算法
kyle~1 天前
排序---插入排序(Insertion Sort)
c语言·数据结构·c++·算法·排序算法
奔跑吧邓邓子1 天前
【C++实战⑦】C++函数实战:从基础到项目应用
c++·实战·函数
张晓~183399481211 天前
短视频矩阵源码-视频剪辑+AI智能体开发接入技术分享
c语言·c++·人工智能·矩阵·c#·php·音视频
纵有疾風起1 天前
数据结构中的排序秘籍:从基础到进阶的全面解析
c语言·数据结构·算法·排序算法
夜猫逐梦2 天前
【Lua】Windows 下编写 C 扩展模块:VS 编译与 Lua 调用全流程
c语言·windows·lua
_OP_CHEN2 天前
数据结构(C语言篇):(十三)堆的应用
c语言·数据结构·二叉树·学习笔记·堆排序··top-k问题
量子炒饭大师2 天前
收集飞花令碎片——C语言关键字typedef
c语言·c++·算法
cellurw2 天前
Linux下C语言实现HTTP+SQLite3电子元器件查询系统
linux·c语言·http