无法使用数组创建C函数

时间:2015-09-22 07:06:35

标签: c arrays string function pointers

我正在尝试为十六进制到二进制转换创建一个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;}

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

1。 scanf("%c",&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))

中的printf即可

3。不要让input保持不变。

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