C语言结构体学生

#include <iostream>
#include <cstring>

struct student {
    int number;
    char *name;
    bool sex;
    struct date {
        int year;
        int month;
        int day;
    } birthday;
};

int main() {
    student stu;
    stu.number = 10020;
    stu.name = new char[100];
    strcpy(stu.name, "wangfang");
    stu.sex = false;
    stu.birthday.year = 1998;
    stu.birthday.month = 11;
    stu.birthday.day = 26;

    std::cout << " 输入学生信息: " << std::endl;
    std::cout << stu.number << " " << stu.name << " ";
    std::cout << (stu.sex? "男" : "女") << " " << stu.birthday.year;
    std::cout << stu.birthday.month << stu.birthday.day << std::endl;

    delete[] stu.name;

    return 0;
}
 

代码分析如下:

The code you've provided is a C++ program that defines a `student` structure with various fields, including a nested `date` structure. The program then creates an instance of `student`, initializes its fields, and prints out the student's information. However, there are a few issues and potential improvements that can be made to the code:

1. Memory leak: The `new char[100]` allocates memory for the `name` field, but there is no corresponding `delete[]` to free this memory. This will cause a memory leak. Fortunately, you have already included a `delete[]` statement at the end of the `main` function, which is correct.

2. Memory allocation for `name`: The `new char[100]` allocates 100 bytes of memory for the `name` field. This is a fixed-size allocation, which may not be sufficient if the name is longer than 99 characters (including the null terminator). It's better to allocate memory based on the actual length of the name string.

3. Using `std::string` for `name`: To avoid manual memory management and potential buffer overflows, it's recommended to use `std::string` for the `name` field.

4. Using `std::cout` for `birthday`: The `birthday` field is a structure, and you're printing it out as if it were an integer. You should print each component of the `date` structure separately.

Here's an improved version of your code:

```cpp
#include <iostream>
#include <cstring>
#include <string>

struct date {
    int year;
    int month;
    int day;
};

struct student {
    int number;
    std::string name; // Use std::string for name
    bool sex;
    date birthday;
};

int main() {
    student stu;
    stu.number = 10020;
    stu.name = "Wang Fang"; // Use a string literal directly
    stu.sex = false;
    stu.birthday.year = 1998;
    stu.birthday.month = 11;
    stu.birthday.day = 26;

    std::cout << "输入学生信息: " << std::endl;
    std::cout << stu.number << " " << stu.name << " ";
    std::cout << (stu.sex ? "男" : "女") << " ";
    std::cout << stu.birthday.year << "-" << stu.birthday.month << "-" << stu.birthday.day << std::endl;

    return 0;
}
```

In this improved version, I've:

- Replaced `char*` with `std::string` for the `name` field.
- Removed the `new` and `delete[]` operations, as `std::string` handles memory allocation and deallocation internally.
- Added a delimiter (`-`) when printing the `birthday` to make it more readable.
- Used `std::endl` instead of `std::cout << std::endl` for better readability.

This version is safer and more idiomatic C++.

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值