简体   繁体   English

当我尝试实现一个反转数组的函数时,为什么输入数组和输出数组都损坏了?

[英]Why is both my input array and output array corrupted when I try to implement a function to reverse the array?

I'm Programming in C, using Linux GCC Compiler. 我正在使用Linux GCC编译器进行C语言编程。 I'm very much new to programming. 我对编程非常陌生。

I'm confused as to why my in[] char array would be changed at all in the function. 我对为什么在函数中完全更改in [] char数组感到困惑。 Doesn't the code simply count the amount of subscripts in in[] and then copy its contents into out[] but backwards? 代码难道不只是简单地计算in []中的下标数量,然后将其内容复制到out []中而是向后复制吗? How is it being changed in the function? 如何在功能中进行更改?

/*reverse in to out*/
void reverse(char in[], char out[]) {
    int i, l,b;
    b = i = l = 0;

    while (in[i] != '\0')
        ++i;

    for(l=i;l > 0; l--) {
        in[l] = out[b];
        ++b;
   }
   return;
}

Your assignment statement is backwards. 您的分配声明是向后的。

in[l] = out[b];

Means "Assign the value in array out at index b to array in index l ". 意味着“分配在阵列的值out在索引b到阵列in的索引l ”。 This line should instead be 该行应改为

out[b] = in[l];

And BTW, you don't need an empty return statement, you can simply omit this in a void function. 顺便说一句,您不需要空的return语句,只需在void函数中省略此语句即可。

for(l=i-1;l >= 0; l--) {
    out[b] = in[l];
    ++b;
}
out[b] = '\0';

Four problems fixed: start at i-1 , test for l>=0 , reverse the assignment, and terminate out with a null character. 解决了四个问题:从i-1开始,测试l>=0 ,逆转分配,并以空字符终止。

Also, a good idea is to use const when a function argument will not be changed. 另外,一个好主意是在不更改函数参数时使用const In this case, const char in[] would let you spot the assignment error because the compiler would give you a compile time error. 在这种情况下, const char in[]将使您发现分配错误,因为编译器将为您提供编译时错误。

You can use strlen : 您可以使用strlen:

for(l=strlen(in), b=0; l>=0; l--)
    out[b++] = in[l];

// ensure null terminated so strlen out works
out[b] = '\0';

This assumes out is wide enough for in AND in is null terminated 这假定out足够宽,并且in终止为null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的输出的第一个元素是错误的,我想编写一个将数组作为输入并反转它的 C 函数 - The first element of my output is wrong where I want to write a C function that takes an array as input and reverse it 函数损坏了我在C中的数组 - the function corrupted my array in C 是什么导致我的数组地址在传递给函数时被破坏(更改)? - What causes my array address to be corrupted (change) when passed to function? 谁能告诉我为什么当我尝试在菜单驱动的数组操作程序中调用 Insert 或 Delete 函数时我的程序会崩溃? - Can anyone tell me why my program crashes when I try to call the Insert or Delete function in a menu driven array manipulation program? 为什么我的程序在尝试打印“值”时访问 binaryString 数组的值? - Why is my program accessing values of the binaryString array when I try to print "value"? 当我尝试重新分配()结构指针数组时,为什么我的 C 程序会崩溃? - Why does my C program crash when I try to realloc() a pointer array of structs? 反向数组,当我除以2时为什么起作用? - Reverse array, why does is work when I divide by 2? 当我尝试用数组填充二进制文件时,它总是为空 - my binary file is always empty when i try to fill it with an array 警告参数名称[当我尝试加载阵列时 - Warning parameter names [When I try to load my array 为什么我的合并功能会输出未排序的数组? - Why does my merge function output an array that is not ordered?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM