pr06017.cpp

来自「c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出」· C++ 代码 · 共 57 行

CPP
57
字号
////////////////////////////////////////
// File Name: pr06017.cpp
////////////////////////////////////////
#include <iostream>
#include <cstdlib>

// Employee record structure.
struct Employee
{
    short int emplno;
    char* name;
    float salary;
};

// Function prototypes.
void ShowEmployee(const Employee* emp);

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    Employee* emp;

    // Allocate memory for an employee record.
    // The function malloc() returns a void pointer,
    // but the program casts the pointer to Employee*.
    emp = (Employee*) std::malloc(sizeof(Employee));

    // If the allocation was successful...
    if (emp != 0)
    {
        // Build an Employee record.
        emp->emplno = 123;
        emp->name = "Jones";
        emp->salary = 37500;

        // Show the record.
        ShowEmployee(emp);

        // Free the memory allocated to the record.
        std::free(emp);
    }

    return 0;
}

////////////////////////////////////////
// Display an Employee record.
////////////////////////////////////////
void ShowEmployee(const Employee* emp)
{
    std::cout << "Empl#:  " << emp->emplno << std::endl;
    std::cout << "Name:   " << emp->name   << std::endl;
    std::cout << "Salary: " << emp->salary << std::endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?