创建结构并传递它的功能

时间:2017-12-28 11:11:27

标签: c++ function struct

我需要一个正确创建函数的帮助,然后将其传递给函数。 我有一段这样的代码:

struct phones {
    char lastname[15];
    char number[8];
    char city[15];
} *list;

void enter(phones[], int);
int main()
{
    int n;
    cout << "Enter the number of users: ";
    cin >> n;
    list = new phones[n];
    enter(list, n);
    system("pause");
    delete[]list;
    return 0;
}
void enter(phones list[], int n) {
    for (int i = 0; i < n; i++)
    {
        cout << "User number " << i + 1 << endl;
        cout << "Enter user's lastname ";
        cin >> list[i].lastname;
        cout << "Enter user's number: ";
        cin >> list[i].number;
        cout << "Enter user's city: ";
        cin >> list[i].city;
    }
}

我的老师告诉我,使用*list创建一个结构并不好。 现在我有一个问题如何正确创建结构,从用户的输入声明一个大小,然后将其传递给函数。

3 个答案:

答案 0 :(得分:1)

您可以在声明struct

后始终定义变量
phones *list;

C ++没有VLA,所以要么使用动态分配,根据你的描述不允许,要么使用C ++中的特殊内容:STL。

创建struct phones的向量可以使它变得非常简单:

#include <vector>

// After input number
std::vector<phones> list(n);

您不必为newdelete而烦恼。

答案 1 :(得分:1)

为了好玩,我重写了你的程序,包括显示读取数据的功能。我就是这样的:

#include <cstdlib>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

struct User {
  char lastname[15];
  char number[8];
  char city[15];
};

void enter(User users[], int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Enter user's lastname: ";
    cin >> users[i].lastname;
    cout << "Enter user's number: ";
    cin >> users[i].number;
    cout << "Enter user's city: ";
    cin >> users[i].city;
  }
}

void show(User users[], int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Lastname: " << users[i].lastname << endl;
    cout << "Number: " << users[i].number << endl;
    cout << "City: " << users[i].city << endl;
  }
}

int main()
{
  int n;
  cout << "Enter the number of users: ";
  cin >> n;
  User* users = new User[n];
  enter(users, n);

  cout << "-- Displaying users: --" << endl;
  show(users, n);

  system("pause");
  delete[] users;
  return 0;
}

答案 2 :(得分:0)

您还可以通过在struct之前使用typedef并使用List

来消除程序中的问题

示例如下:

typedef struct Person {
  char lastname[15];
  char number[8];
  char city[15];
} * List;

void enter(List list, int n) ;
void show(List list, int n);
int main()
{
  int n;
  cout << "Enter the number of users: ";
  cin >> n;
  List users = new Person[n];
  enter(users, n);
  cout << "-- Displaying users: --" << endl;
  show(users, n);
  system("pause");
  delete[] users;
  return 0;
}

void show(List list, int n)
{
  for (int i = 0; i < n; ++i) {
    cout << "[User number " << (i + 1) << "]" << endl;
    cout << "Lastname: " << list[i].lastname << endl;
    cout << "Number: " << list[i].number << endl;
    cout << "City: " << list[i].city << endl;
  }

}

void enter(List list, int n) {
    for (int i = 0; i < n; i++)
    {
        cout << "User number " << i + 1 << endl;
        cout << "Enter user's lastname ";
        cin >> list[i].lastname;
        cout << "Enter user's number: ";
        cin >> list[i].number;
        cout << "Enter user's city: ";
        cin >> list[i].city;
    }
}