C 试图反转数组中的多个整数-想知道为什么我的代码不';行不通

C 试图反转数组中的多个整数-想知道为什么我的代码不';行不通,c,arrays,C,Arrays,我可能在这里编写了一些非常糟糕/低效的代码,但到目前为止,我一直在尝试从头开始构建 在这段代码中,我要做的是让用户输入8个整数,然后反转它们。 这意味着如果用户输入12 21 32 43 54 43 23 54,程序将输出45 32 34 45 34 23 12 21 这是我编写的代码: #include <stdio.h> #include <string.h> int main(void){ int input[8]; char co

可能在这里编写了一些非常糟糕/低效的代码,但到目前为止,我一直在尝试从头开始构建

在这段代码中,我要做的是让用户输入8个整数,然后反转它们。 这意味着如果用户输入
12 21 32 43 54 43 23 54
,程序将输出
45 32 34 45 34 23 12 21

这是我编写的代码:

#include <stdio.h>
#include <string.h>

int main(void){
        int input[8];
        char code[20][8];
        char rev[20][8];
        char pos[3];

        for(int i = 0; i < 8; i++){
                int j = i+1;
                if(j==1){
                        strncpy(pos, "st", 3);
                }
                else if(j==2){
                        strncpy(pos, "nd", 3);
                }
                else if(j==3){
                        strncpy(pos, "rd", 3);
                }
                else{
                        strncpy(pos, "th", 3);
                }

                printf("Enter the %d%s integer : ",i+1,pos);
                scanf("%d", &input[i]);
                sprintf(code[i],"%d",input[i]);
        }   

        for(int i = 0; i < 8; i++){
                for(int j = 0; j < sizeof(code[i]);j++){
                        rev[i][sizeof(code[i]-j)] = code[i][j]; 
                }
        }

        printf("NORMAL OUTPUT \t\t:");
        for(int i = 7; i >= 0;i--){
                for(int j = 0; j < sizeof(rev[i]);j++){
                        printf("%c",rev[i][j]);
                }
                printf(" ");
        }
        printf("\n");

        printf("NON-REVERSE OUTPUT \t:");
        for(int i = 7; i >= 0;i--){
                for(int j = 0; j < sizeof(code[i]);j++){
                        printf("%c",code[i][j]);
                }
                printf(" ");
        }
        printf("\n");

        printf("INTEGER OUTPUT \t\t:");
        for(int i = 7; i >= 0;i--){
                printf("%d",input[i]);
                printf(" ");
        }
        printf("\n");

}
尝试2,使用4位整数:

Enter the 1st integer : 1234
Enter the 2nd integer : 1234
Enter the 3rd integer : 1234
Enter the 4th integer : 1234
Enter the 5th integer : 1234
Enter the 6th integer : 1234
Enter the 7th integer : 1234
Enter the 8th integer : 1234
NORMAL OUTPUT       :     ¤Aÿ   
NON-REVERSE OUTPUT  :1234 1234 1234 1234 1234 1234 1234 1234 
INTEGER OUTPUT      :1234 1234 1234 1234 1234 1234 1234 1234
Enter the 1st integer : 12345678
Enter the 2nd integer : 12345678
Enter the 3rd integer : 12345678
Enter the 4th integer : 12345678
Enter the 5th integer : 12345678
Enter the 6th integer : 12345678
Enter the 7th integer : 12345678
Enter the 8th integer : 12345678
NORMAL OUTPUT       :8 8 8 8 8 8®ßôý 8  
NON-REVERSE OUTPUT  :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678 
INTEGER OUTPUT      :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678 
尝试使用8位整数的3:

Enter the 1st integer : 1234
Enter the 2nd integer : 1234
Enter the 3rd integer : 1234
Enter the 4th integer : 1234
Enter the 5th integer : 1234
Enter the 6th integer : 1234
Enter the 7th integer : 1234
Enter the 8th integer : 1234
NORMAL OUTPUT       :     ¤Aÿ   
NON-REVERSE OUTPUT  :1234 1234 1234 1234 1234 1234 1234 1234 
INTEGER OUTPUT      :1234 1234 1234 1234 1234 1234 1234 1234
Enter the 1st integer : 12345678
Enter the 2nd integer : 12345678
Enter the 3rd integer : 12345678
Enter the 4th integer : 12345678
Enter the 5th integer : 12345678
Enter the 6th integer : 12345678
Enter the 7th integer : 12345678
Enter the 8th integer : 12345678
NORMAL OUTPUT       :8 8 8 8 8 8®ßôý 8  
NON-REVERSE OUTPUT  :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678 
INTEGER OUTPUT      :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678 
整数位数越多,非反转输出的故障越小,但正常输出始终中断。我不知道这可能是什么原因,除非问题与地址有关

我知道可能有更简单的方法来反转多个整数或字符串,但我在这里的主要希望是找出这段代码到底出了什么问题,是整个结构/策略有缺陷,还是我忘记了解释什么

对不起,这篇文章太长了,我只是想把所有的信息都放在那里,以防万一,或者其他人需要帮助

更新1:有人建议我使用strlen而不是sizeof,并在声明数组时将8更改为9,这样做很有效。。。稍微

使用相同的输入,以下是与之前相同的尝试:

尝试1:

NORMAL OUTPUT       :      2Øp h1°ü   
NON-REVERSE OUTPUT  :8 7 6 5 4 3 2 1 
INTEGER OUTPUT      :8 7 6 5 4 3 2 1
尝试2:

NORMAL OUTPUT       :234 4   4 4 4µê12 34Ýæ1
NON-REVERSE OUTPUT  :1234 1234 1234 1234 1234 1234 1234 1234 
INTEGER OUTPUT      :1234 1234 1234 1234 1234 1234 1234 1234 
尝试3:

NORMAL OUTPUT       :2345678 2345678 2345678ý12345678 2345678 2345678 2345678 2345678 6785ý
NON-REVERSE OUTPUT  :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678
INTEGER OUTPUT      :12345678 12345678 12345678 12345678 12345678 12345678 12345678 12345678 
正常输出的问题仍然存在,反转像往常一样被打破,除了最后一个看起来(尽管没有反转)至少有实际数字的输出

代码的所有更新将显示在此处,顺便说一句:

以防有人想要更新

UPDATE2:正如BLUEPIXY在评论中所说,我需要为空终止符留出空间
strlen
正在计算空终止符,因此当我到达反转整数的块时,它基本上覆盖了空终止符,破坏了整个字符串

除此之外,我还必须初始化正在反转的2D数组,这可以通过将其设置为{{0}来完成。我的要点是,对于每个维度,您必须再添加一个{}

固定的代码是在要点链接,谢谢

您的版本经过简化和更正: 我在github中使用了您的最终代码,并发现它可以简化<代码>字符版本[20][9]根本不需要

另外,我认为
char-code[20][9]
应该是
char-code[8][20]
。8个字符串形式的数字,长度为19位(第20项表示
\0
字符)

输出

Original array: 12 21 32 43 54 43 23 54 
Reversed array: 45 32 34 45 34 23 12 21 

使用字符串的长度
strlen
。要使用
sprintf
将八位数字写成字符串,您需要
9
区域,而不是
8
。我会更新帖子<代码>strlen(代码[i]-j)
strlen(rev[i])
rev[i]
必须以空字符(
'\0'
)终止。因此
strlen(code[i]-j)
-->
strlen(code[i])-j-1
char rev[20][9]-->
charrev[20][9]={{{0}
{{0}
将2D数组初始化为
0
。(另一种方法是通过在末尾复制
'\0'
来设置它。)第二种方法似乎使用了一种数学方法来反转整数,这很酷。虽然*结尾部分有点乱,但考虑到4将返回第4个。@txtman我现在已经检查过了,第四个提示返回
4th
输入第一个整数:123输入第二个整数:345输入第三个整数:456输入第四个整数:234
。啊,对不起,我忘记了数组从0开始的事实。
Original array: 12 21 32 43 54 43 23 54 
Reversed array: 45 32 34 45 34 23 12 21