-1
#include <string.h>

using namespace std;

void rc4(unsigned char * ByteInput, unsigned char * pwd,
    unsigned char * &ByteOutput){
    unsigned char * temp;
    int i,j=0,t,tmp,tmp2,s[256], k[256];
    for (tmp=0;tmp<256;tmp++){
        s[tmp]=tmp;
        k[tmp]=pwd[(tmp % strlen((char *)pwd))];
    }
    for (i=0;i<256;i++){
        j = (j + s[i] + k[i]) % 256;
        tmp=s[i];
        s[i]=s[j];
        s[j]=tmp;
    }
    temp = new unsigned char [ (int)strlen((char *)ByteInput) + 1 ] ;
    i=j=0;
    for (tmp=0;tmp<(int)strlen((char *)ByteInput);tmp++){
        i = (i + 1) % 256;
        j = (j + s[i]) % 256;
        tmp2=s[i];
        s[i]=s[j];
        s[j]=tmp2;
        t = (s[i] + s[j]) % 256;
        if (s[t]==ByteInput[tmp])
            temp[tmp]=ByteInput[tmp];
        else
            temp[tmp]=s[t]^ByteInput[tmp];
    }
    temp[tmp]=' ';
    ByteOutput=temp;
}

int main()
{
    unsigned char data[256] = "hello";
    unsigned char pwd[256] = "2147124912";
    unsigned char output[256];
    rc4(data,pwd,*output); 

    return 0;
}

meme@ubuntu:~/CSCI368$ g++ try.cpp -o try
try.cpp: In function ‘int main()’:
try.cpp:42:20: error: invalid initialization of non-const reference of type ‘unsigned char*&’ from an rvalue of type ‘unsigned char*’
try.cpp:5:6: error: in passing argument 3 of ‘void rc4(unsigned char*, unsigned char*, unsigned char*&)’

コンパイルしようとしていますが、引数 3 に問題があると思いますrc4(data,pwd,*output);

として渡すにはどうすればよいですかunsigned char*&

4

2 に答える 2

2
unsigned char* output;
rc4(data,pwd,output); 

いいえ

unsigned char output[256];
rc4(data,pwd,*output); 

しかし、上記のコメントが言っているように、ポインターを理解していないのに、なぜポインターを使用するのでしょうか? std::stringこのコードは、 and/orを使用することで、より簡単に記述でき、バグが少なくなりstd::vectorます。

于 2013-05-05T08:35:45.183 に答える