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

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

相关推荐
QXH20000023 分钟前
数据结构—单链表
c语言·开发语言·数据结构
David猪大卫1 小时前
数据结构修炼——顺序表和链表的区别与联系
c语言·数据结构·学习·算法·leetcode·链表·蓝桥杯
Iceberg_wWzZ1 小时前
数据结构(Day14)
linux·c语言·数据结构·算法
微尘81 小时前
C语言存储类型 auto,register,static,extern
服务器·c语言·开发语言·c++·后端
liangbm31 小时前
MATLAB系列05:自定义函数
开发语言·笔记·matlab·教程·函数·自定义函数·按值传递
五味香1 小时前
C++学习,动态内存
java·c语言·开发语言·jvm·c++·学习·算法
Aurora20052 小时前
蓝桥杯2024省C
c语言·算法·蓝桥杯
蟹至之2 小时前
字符函数 和 字符串函数 的使用与模拟
c语言·字符串·指针·const关键词
LaoWaiHang4 小时前
C语言从头学61——学习头文件signal.h
c语言
一名路过的小码农4 小时前
C/C++动态库函数导出 windows
c语言·开发语言·c++