【问题标题】:Could not creat C function using array无法使用数组创建 C 函数
【发布时间】:2015-09-22 07:06:35
【问题描述】:

我正在尝试为十六进制到二进制转换创建一个 c 函数,但出现奇怪的错误,我对 C 很陌生。尝试传递字符串但没有运气得到一堆错误。

#include<stdio.h>

#include<string.h>
char *hexc(char hex[10]);

int main(){
char hex[10];
//char bin[1000];
scanf("%c",&hex[10]);
printf("binary value: %s",hexc(hex[10]));}

char *hexc(char hex[10])
{
const char binary[16][5] = {"0000", "0001", "0010", "0011",      "0100",  "0101","0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110","1111"};
const char digits[] = "0123456789abcdef";
const char input[100]; // input value
memcpy (input,hex,100);
char res[1024];
res[0] = '\0';
int p = 0;
int value =0;

while(input[p])
    {
        const char *v = strchr(digits, tolower(input[p]));
        if(v[0]>96){
            value=v[0]-87;
        }
        else{
            value=v[0]-48;
        }
        if (v){
            strcat(res, binary[value]);
        }
        p++;
    }
    printf("binary: %s", res); 
    return res;}

错误列表,尝试了多次谷歌搜索但没有运气。

test2.c: In function ‘main’:
test2.c:9:1: warning: passing argument 1 of ‘hexc’ makes pointer from integer without a cast [enabled by default]
 printf("binary value: %s",hexc(hex[10]));}
 ^
test2.c:3:7: note: expected ‘char *’ but argument is of type ‘char’
 char *hexc(char hex[10]);
       ^
test2.c: In function ‘hexc’:
test2.c:16:1: warning: passing argument 1 of ‘memcpy’ discards ‘const’ qualifier from pointer target type [enabled by default]
 memcpy (input,hex,100);
 ^
In file included from test2.c:2:0:
/usr/include/string.h:46:14: note: expected ‘void * __restrict__’ but argument is of type ‘const char *’
 extern void *memcpy (void *__restrict __dest, const void *__restrict __src,
              ^
test2.c:36:28: warning: function returns address of local variable [-Wreturn-local-addr]
 printf("binary: %s", res); return res;}

【问题讨论】:

  • 阅读几本不错的C编程书籍
  • 这里:scanf("%c",&amp;hex[10]);,越界访问。这是未定义的行为,所有赌注都已取消。你必须解决这个问题。
  • 编译器的哪个错误你不明白?
  • hexc(hex[10])) 也是越界访问。
  • 专业提示:问题是一系列以问号结尾的单词,经过精心设计,我们知道您在寻找什么信息。没有您的问题,所以我们不知道您在寻找什么信息。也许您的意思是就您可能不理解的警告提出问题?与其强迫我们把你当成你不懂任何东西(你真的希望我们那样对待你吗?),为什么不告诉我们这些警告中的哪些词引起了你的困惑呢?这将为我们节省大量时间,并可能为您节省大量情绪困扰。

标签: c arrays string function pointers


【解决方案1】:
#include<stdio.h>
#include <ctype.h> // add to use tolower function
#include<string.h>
char *hexc(char hex[10]); // note: 10 in here is ignored by compiler

int main(){
    char hex[10];
    //char bin[1000];
    //scanf("%c",&hex[10]); // hex[10] is out of range
    scanf("%9s",hex);
    //printf("binary value: %s",hexc(hex[10]));} // hex[10] is out of range
    printf("binary value: %s",hexc(hex));
    return 0; // it is better to return something
}

char *hexc(char hex[10])
{
    const char binary[16][5] = {"0000", "0001", "0010", "0011",      "0100",  "0101","0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110","1111"};
    const char digits[] = "0123456789abcdef";
    char input[100]; // input value // don't use const here. it is not needed and do harm.
    //memcpy (input,hex,100); // 100 is too large to read from hex
    memcpy (input,hex,10);
    static char res[1024]; // make this static so that it can be read from the main function safely (this may not be thread-safe)
    res[0] = '\0';
    int p = 0;
    int value =0;

    while(input[p])
    {
        const char *v = strchr(digits, tolower(input[p]));
        if(v[0]>96){
            value=v[0]-87;
        }
        else{
            value=v[0]-48;
        }
        if (v){
            strcat(res, binary[value]);
        }
        p++;
    }
    printf("binary: %s", res); 
    return res;
}

【讨论】:

  • 感谢@MikeCAT,我尝试了您的更改并留下了一个错误,net.c:43:5: error: expected declaration or statement at end of input return res;
  • 此代码do work with no compile errors or warnings。请再次检查您的代码。
【解决方案2】:

1. scanf("%c",&amp;hex[10]);

这是错误并导致未定义的行为。数组索引从 0 开始,因此您可以访问的最高索引是 9 而不是 10

使用fgets 接受输入 -

fgets(hex,10,stdin);

2.printf拨打电话时

 hexc(hex[10]));              // hex[10] index out of bounds 

hexc 需要 char array 并将其传递给单个字符。

只需将数组 hex 传递给它 - hexc(hex)) in printf

3. 不要让input 保持不变。

    char input[100]; //   char array (without const)
    memcpy (input,hex,100);

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    相关资源
    最近更新 更多