在函数中通过引用从数组复制到数组

Copy from array to array by refenrence in function

本文关键字:数组 复制 引用 函数      更新时间:2023-10-16

我不知道为什么它不起作用。更重要的是,我甚至不能说错误是关于什么;/有人能解释一下错误是关于什么的吗?

代码应该:创建一个像"妈妈"这样的字符串。然后创建二维数组,用字符串填充它。空闲空间用_填充。所以妈妈盒子=

[m] [o]

[m] [_]

现在用列后面的文本填充下一个数组。填充到新数组的Mom_将看起来像mmo_。然后我计算加密文本。我希望你明白我做了什么:D

这里是代码

//wal = kolumny=wiersze
#include <cstdlib>
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
void pole(int &a,const int &l);
void tab(const char &s[],char &d[], char &f[],const int a);
int main(){
    string code;
    cin >> code;
    int wall=1;
    int d=code.length();
    char tekst[d];   
    pole(wall,d);
    strcpy(tekst,code);
    char kw[wall][wall];
    char szyfr[d];
    tab(tekst,kw,szyfr,wall);   
    for (int i=0;i<d;i++) 
    cout << szyfr[i] << endl;
    system("PAUSE");
    return 0;
}
void pole(int &a,const int &l){
    if (a*a < l)
    pole(a+=1,l);
}
void tab(const char &s[],char &d[], char &f[],const int a){
    int i=0;
    for (int x=0;x<a;x++,i++){
        for (int y=0;y<a;y++,i++){
            if(s[i])
            d[x][y]=s[i];
            else d[x][y]=='_';
            f[i]=d[x][y];
        }
    }
}

d[x][y]tab中没有意义。您必须将第一个维度作为参数传递,并在索引时使用它。比如:

void tab(const char &s[],char* &d, char &f[],const int a, int d_num_cols){
    int i=0;
    for (int x=0;x<a;x++,i++){
        for (int y=0;y<a;y++,i++){
            if(s[i])
            d[x*d_num_cols + y]=s[i];
            else d[x*d_num_cols + y]=='_';
            f[i]=d[x*d_num_cols + y];
        }
    }
}