Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 通过指针传回更新的字符数组_C_Arrays_Pointers_Char - Fatal编程技术网

C 通过指针传回更新的字符数组

C 通过指针传回更新的字符数组,c,arrays,pointers,char,C,Arrays,Pointers,Char,我试图将更新后的char数组传回原始的main()函数,但是当我试图在数组上复制一个指针时,在它给我“char”之前,它的间接寻址级别与char()[15]有所不同 这是一个ISBN验证程序 这是: int main(void) { int divisible; char choice, trash; char code[15]; do { printf("Enter an ISBN (book) number:\n");

我试图将更新后的char数组传回原始的
main()
函数,但是当我试图在数组上复制一个指针时,在它给我“char”之前,它的间接寻址级别与char()[15]有所不同

这是一个ISBN验证程序

这是:

int main(void)
{
    int divisible;
    char choice, trash;
    char code[15];


    do
    {
        printf("Enter an ISBN (book) number:\n");
        fgets(code, 15, stdin);

        printf("The ISBN entered is:%s\n", code);
        divisible = testISBN(&code);
        if (divisible == 1)
            printf("\n\n\tValid ISBN Number\n");
        else
            printf("\n\n\tInvalid ISBN Number\n");

        printf("\nDo you wish to continue? (Y/N)\n");
        choice = getchar();
        trash = getchar();
    } while (toupper(choice) != 'N');

    return 0;
}


int testISBN(char *code)
{
    int i;
    int sum = 0;
    int weight = 10;
    char codefinal[10];
    int x = 0;
    for (i = 0; i<15; i++)
    {
        int chVal;
        if ((i == 9) && (toupper(code[i]) == 'X'))
        {
            printf("%c",code[i]);
            chVal = 10;
        }
        else
        {
            chVal = code[i] - '0';
        }
        if (chVal == 0 || chVal == 1 || chVal == 2 || chVal == 3 || chVal == 4 || chVal == 5 || chVal == 6 || chVal == 7 || chVal == 8 || chVal == 9) {
            char y = (char)chVal;
            codefinal[x] = y;
            x++;
        }

        sum += chVal * weight;
        weight--;
    }
    printf("sum is %d", sum);

    for  (i = 0; i < 15; i++) {
        *code[i] = codefinal[i];
        printf("%c", code);
    }
    return (sum % 11) == 0;
}
int main(无效)
{
整除的;
煤焦选择,垃圾;
字符码[15];
做
{
printf(“输入ISBN(图书)编号:\n”);
fgets(代码15,标准DIN);
printf(“输入的ISBN为:%s\n”,代码);
可除=测试编号(&代码);
如果(可除==1)
printf(“\n\n\tValid ISBN编号\n”);
其他的
printf(“\n\n\t有效的ISBN编号\n”);
printf(“\N是否继续?(是/否)\N”);
choice=getchar();
trash=getchar();
}while(toupper(choice)!=“N”);
返回0;
}
int testISBN(字符*代码)
{
int i;
整数和=0;
整数权重=10;
字符编码最终[10];
int x=0;

对于(i=0;i只需按原样传递代码,它将衰减为指针

char码[15];
可除=testISBN(代码);
您可能需要将testISBN签名更改为

int testISBN(字符代码[15]);
这样,如果索引搞砸了,就有更高的机会得到编译器警告

简单的例子:

#包括
void func(字符arr[3]){
arr[0]='H';
arr[1]=“i”;
arr[2]=0;
}
int main(){
char arr[3]={0};
func(arr);
printf(“%s”,arr);
}
打印“嗨”



正如评论员提到的,您的代码还存在其他问题,如内存越界访问。为了使您的生活更轻松,请花一些时间阅读编译器手册,并学习如何打开所有警告,它们在大多数情况下都表示代码有问题。

对代码进行以下更改:

  • 将函数
    int testISBN(char*code)
    的原型更改为
    int testISBN(char code[])
  • 将调用从
    testISBN(&code);
    更改为
    testISBN(code);
  • 将行
    *code[i]=codefinal[i];
    更改为
    code[i]=codefinal[i];
  • 在第
    printf(“%c”,code);
    行中,您打印的不是
    code[]
    的值,而是地址。因此,将其更改为
    printf(“%c”,code[i]);
    注意
    code[]
    也包含一些不可打印的字符

  • 建议:如果(chVal==0 | | chVal==1 | | chVal==2 | | chVal==3 | | chVal==4 | chVal==5 | | chVal==6 | chVal==7 | chVal==8 | | chVal==9)
可以更改为如果(chVal>=0 | chVal==5 | chVal==6 | chVal==10)代码不存在的话(
14)。这是你的最终代码;
也是错误的。
code
是一个数组,因此它作为指向函数调用的第一个元素的指针自动衰减。删除
&
--编译器应该警告您指针类型不匹配。此语法错误:
*code[i]=codefinal[i];
两者都不能解决我的问题。因此有一个问题,它没有将其存储为“值”?我根本无法打印它。它将其存储在codefinal中,错误地说它应该说“0”而不是空值:您打印的不是值而是
code[]
的地址。我已经更新了相同的答案。