简体   繁体   English

如何用几种方法处理1个数组

[英]How to work with 1 array with few methods

I have Multidimensional array in my struct i want to work with this array using few methods, i think i should use double pointer but i have no idea how to do it 我的结构中有多维数组,我想使用几种方法使用此数组,我认为我应该使用双指针,但是我不知道该怎么做

struct generator {
    char r[26][max];

        void set();
    void display();
};

void generator::set() {
    char *tab = new char[max];
    int k = 0;

    cin >> tab;

    while (tab[k] != '\0') {    
        r[0][k] = tab[k];
        k++;
}
void generator::display(){
    cout << r[0][1];    // should display first letter of string
}

in

  cout << r[0][1]; // should display first letter of string 

the comment is wrong, that displays the second letter, first letter is at index 0 注释错误,显示第二个字母,第一个字母位于索引0


Adding enough definitions and missing '}' to compile and execute : 添加足够的定义并缺少'}'以进行编译和执行:

#include <iostream>
using namespace std;

#define max 10

struct generator {
  char r[26][max];

  void set();
  void display();
};

void generator::set() {
  char *tab = new char[max];
  int k = 0;

  cin >> tab;

  while (tab[k] != '\0') {    
    r[0][k] = tab[k];
    k++;
  }
}

void generator::display(){
  cout << r[0][1];    // should display first letter of string
}

int main()
{
  generator g;
  g.set();
  g.display();
  cout << endl;
}

Compilation and execution : 编译执行:

/tmp % g++ -pedantic -Wall -Wextra g.cc
vxl15036 /tmp % ./a.out
aze
z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM