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

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

相关推荐
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔16 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing1023292916 小时前
Day03_刷题niuke20250915
c语言
第七序章19 小时前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
l1t21 小时前
利用DeepSeek实现服务器客户端模式的DuckDB原型
服务器·c语言·数据库·人工智能·postgresql·协议·duckdb
l1t1 天前
利用美团龙猫用libxml2编写XML转CSV文件C程序
xml·c语言·libxml2·解析器
Gu_shiwww1 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
你怎么知道我是队长1 天前
C语言---循环结构
c语言·开发语言·算法
程序猿编码1 天前
基于 Linux 内核模块的字符设备 FIFO 驱动设计与实现解析(C/C++代码实现)
linux·c语言·c++·内核模块·fifo·字符设备
mark-puls1 天前
C语言打印爱心
c语言·开发语言·算法