c++ - 在 C++ 中通过引用传递 unsigned char 数组

标签 c++ arrays pass-by-reference

全部!我在实现 Serpent 密码的学校项目中遇到了一些困难。问题出在函数上

setKey(unsigned char (&user_key[32]))

我想将一个大小为 32 的字节数组传递给我的函数,然后让我的函数对该数组的值执行各种操作。我打印的代码无法编译,因为我收到错误 -

no known conversion for argument 1 from ‘unsigned char (*)[32]’ to 
‘unsigned char (&)   [32]’

我浏览了许多类似的帖子,但我发现的解决方案似乎都没有导致我的代码编译,或者即使编译了,也没有按照我的意愿进行。不幸的是,我不记得我上次是如何编译我的代码的,但是当我这样做时,在函数 setKey() 中打印出 user_key[] 的值给了我以下输出:

19, 0, 0, 0, 51, 0, 0, 0, 83, 0, 0, 0, 115, 0, 0, 0, 20, 0, 0, 0, 52, 0, 0, 0,
84, 0, 0, 0, 116, 0, 0, 0  

此外,当它运行时,它在打印出该行后以段错误退出 “这是结束。我唯一的 friend ,结束。”以及“检测到堆栈粉碎”警告。简而言之,我的代码似乎有各种各样的错误。任何帮助将不胜感激!!

#include <iostream>
#include <math.h>

using namespace std;

class KeySchedule{

  int ip[64];
  int key_size;
  unsigned long long int k0;
  unsigned long long int k1;
  unsigned long long int k2;
  unsigned long long int k3;

public:

  KeySchedule(){

    /*The initial permutation. To be applied to the plaintext and keys.
      ip = {0, 32, 64, 96, 1, 33, 65, 97, 2, 34, 66, 98, 3, 35, 67, 99,
      4, 36, 68, 100, 5, 37, 69, 101, 6, 38, 70, 102, 7, 39, 71, 103,
      8, 40, 72, 104, 9, 41, 73, 105, 10, 42, 74, 106, 11, 43, 75, 107,
      12, 44, 76, 108, 13, 45, 77, 109, 14, 46, 78, 110, 15, 47, 79, 111,
      16, 48, 80, 112, 17, 49, 81, 113, 18, 50, 82, 114, 19, 51, 83, 115,
      20, 52, 84, 116, 21, 53, 85, 117, 22, 54, 86, 118, 23, 55, 87, 119,
      24, 56, 88, 120, 25, 57, 89, 121, 26, 58, 90, 122, 27, 59, 91, 123,
      28, 60, 92, 124, 29, 61, 93, 125, 30, 62, 94, 126, 31, 63, 95, 127};
    */

    for (int i = 0; i < 127; i++ ){
      ip[i] = (32*i) % 127;
    } 
    ip[127] = 127;

    k3 = 0; 
    k2 = 0; 
    k1 = 0;
    k0 = 0;  
    key_size = -1;
  }

  void setKey (unsigned char (&user_key)[32]){

    for (int i = 0; i<32; i++){
      cout << (int)(user_key)[i] << endl;
    }

    for (int i = 0; i<8; i++){
      k3 ^= (int)(user_key)[i] << (8-i);
      k2 ^= (int)(user_key)[i+8] << (8-i);
      k1 ^= (int)(user_key)[i+16] << (8-i);
      k0 ^= (int)(user_key)[i+24] << (8-i);
    }

  }
};

int main(){

   unsigned char testkey[]= {0x01, 0x02, 0x03, 0x04, 
                 0x05, 0x06, 0x07, 0x08, 
                 0x09, 0x0a, 0x0b, 0x0c, 
                 0x0d, 0x0e, 0x0f, 0x10,
                 0x20, 0x30, 0x40, 0x50, 
                 0x60, 0x70, 0x80, 0x90, 
                 0xa0, 0xb0, 0xc0, 0xd0, 
                 0xe0, 0xf0, 0x00, 0xff};

   cout << "The size of testkey is: " 
        << sizeof(testkey)/sizeof(*testkey) << endl;

   KeySchedule ks = KeySchedule();

   ks.setKey(&testkey);
   cout << "This is the end. My only friend, the end." << endl;
};

最佳答案

您正在传递您的 unsigned char 数组的地址,即指向 unsigned char[32] 的指针。你需要这个:

ks.setKey(testkey);

关于c++ - 在 C++ 中通过引用传递 unsigned char 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19610759/

相关文章:

javascript - 将数据从 JSON 推送到 Angular 4 组件中的数组

C++ 指针传递和值修改

javascript - JavaScript 是按引用传递还是按值传递语言?

Java 引用传递不起作用?

C++ 对象构造函数通过 const 引用复制传递

c++ - 如果 T 是一个函数,不要对 T 使用 sizeof

c++ - 在链表前面插入节点

c - 为什么二维不等于一维

c++ - 为什么不尽可能使用早期绑定(bind)?

php - 如何使用数组函数合并 php 中的内部数组元素?