【问题标题】:Copy from array to array by refenrence in function在函数中通过引用从数组复制到数组
【发布时间】:2013-03-05 11:25:45
【问题描述】:

我不知道为什么它不起作用。更重要的是我什至不能说什么是错误;/ 谁能解释一下什么是错误?

代码应该是: 创建一个带有单词 like - mom 的字符串。 然后创建二维数组以用字符串填充它。空闲空间用_填充。所以妈妈框=

[米] [o]

[米] [_]

现在用列后面的文本填充下一个数组。填充到新数组的 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];
        }
    }
}

【问题讨论】:

  • 您正在将 C++ 与 C 混合使用。C++ 没有 VLAs

标签: c++ string function reference char


【解决方案1】:

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

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];
        }
    }
}

【讨论】:

    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多