gpt4 book ai didi

无法使用数组创建 C 函数

转载 作者:行者123 更新时间:2023-11-30 20:24:45 26 4
gpt4 key购买 nike

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

最佳答案

#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;
}

关于无法使用数组创建 C 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com