【问题标题】:Error C2113 when compiling C code编译 C 代码时出现错误 C2113
【发布时间】:2015-10-22 02:01:25
【问题描述】:

我对 C 没有任何经验(尽管我经常使用 C#),但我希望编译一些 C 代码:

http://practicalcryptography.com/cryptanalysis/stochastic-searching/cryptanalysis-bifid-cipher/

当我在 VS2015 中使用开发者命令提示符用 cl.exe 编译它时,我遇到了一个错误:

c:\simple>cl bifidcrack.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23026 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

bifidcrack.c
bifidcrack.c(113): error C2113: '-': pointer can only be subtracted from another
 pointer
bifidcrack.c(114): error C2113: '-': pointer can only be subtracted from another
 pointer

c:\simple>

为什么编译失败?其他人(在页面 cmets 中)据说编译它没有任何问题。

C:\简单包含:

C:\simple>dir
 Volume in drive C is OS
 Volume Serial Number is AA86-3F24

 Directory of C:\simple

22/10/2015  02:50    <DIR>          .
22/10/2015  02:50    <DIR>          ..
12/10/2015  12:06             4,491 bifidcrack.c
12/10/2015  12:06         7,301,392 qgr.h
12/10/2015  12:06               574 scoreText.c
12/10/2015  12:06                44 scoreText.h
               4 File(s)      7,306,501 bytes
               2 Dir(s)  14,560,649,216 bytes free

C:\simple>

根据要求,以下是有问题的行:

char *bifidDecipher(char *key, int period, char *text, char *result, int len){
    int i, j;
    char a,b; /* the digram we are looking at */
    int a_ind,b_ind;
    int a_row,b_row;
    int a_col,b_col;

    for (i = 0; i < len; i += period){
        if (i + period > len){
            period = len - i;
        }
        for (j = 0; j < period; j ++){
            a = text[i+(j/2)];
            b = text[i+((period+j)/2)];

            /*if (index(key,a) == NULL || index(key,b) == NULL) break;*/
113         a_ind = (int)(index(key,a) - key);
114         b_ind = (int)(index(key,b) - key);
            a_row = a_ind / 5;
            b_row = b_ind / 5;
            a_col = a_ind % 5;
            b_col = b_ind % 5;
            if (j % 2 == 0){
                result[i+j] = key[5*a_row + b_col];
            } else {
                result[i+j] = key[5*a_col + b_row];
            }
        }
    }
    result[i] = '\0';
    return result;
}

【问题讨论】:

  • 引用的代码行会很有用...
  • 添加了 bifidcrack.c 的 sn-p 并列出了第 113 行和第 114 行。希望对您有所帮助!
  • 需要知道“索引”是什么。如果它没有返回 char *,那么是的,这是一个编译错误。
  • 感谢您的帮助。我将如何去寻找它是什么?不幸的是,我没有任何 C 经验。我没有注意到我发布的链接现在已关闭,但如果有帮助,它仍然可以通过 TWBM 访问:web.archive.org/web/20151018181750/http://…

标签: c compilation compiler-errors


【解决方案1】:

错误是在 bifidcrack.c 的第 113 行和第 114 行,一个指针被用来从一个非指针元素中减去。没有看到实际代码,由于页面不可用,我猜是有一个不正确的解引用指针。

类似于这个场景的东西

int * pointer_int = 5;
int non_pointer = 4;

这就是它现在的样子

int new_int = non_pointer - pointer_int;

虽然它应该是这样的

int new_int = non_pointer - (int*)pointer_int;

您必须打开文件并转到这些行以查看有关更改它们的信息。

【讨论】:

  • 对不起,链接刚刚断开,我没有注意到。我已经添加了 bifidcrack.c 的一部分,其余的可以通过 TWBM 访问:web.archive.org/web/20151018181750/http://…我不了解 C,但我会尝试修改代码。感谢您的建议。
  • 因为 key 是一个指针,所以返回的任何索引也需要是一个指针。那是你的错误。您可以尝试在每一行的最后一个键之前添加 (char*)
  • 谢谢,但我添加了演员表,编译时它返回了同样的错误。
  • linux.die.net/man/3/index index 如果它匹配一个字符,则返回一个指针,但如果它不匹配一个字符,它返回 NULL.... 尝试抛出一些 if 语句来处理这个问题。像 if(index(key,a) != NULL){a_ind = (int)(index(key,a) - key);} else{ DO SOMETHING TO HANDLE IT} 和第 114 行一样。取决于编译器可能过于谨慎。
【解决方案2】:

您引用的代码包含折旧代码,如果不从 index(折旧 POSIX)更改为 strchr(current) 并酌情进行强制转换,将无法编译。

示例:

 // https://www.cplusplus.com/reference/cstring/strchr/
 a_ind = (int)(strchr(key, (int)a) - (const char *)key );
 b_ind = (int)(strchr(key, (int)b) - (const char *)key );

编译:cl /EHcs bifidcrack.c scoreText.c

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多