中打印未调整大小的数组

中打印未调整大小的数组

Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我是C语言的新手,试图弄清楚如何在C语言中打印未调整大小的数组。使用以下代码,我看起来很奇怪,但无法弄清楚原因。

我需要一些帮助:

main()
{
    int i;
    char *prt_1st;
    char list_ch[][2] = {'1','a', '2','b', '3','c','4','d','5','e','6','f' };

    prt_1st = list_ch;
    for (i = 0; i < sizeof(list_ch); i++) {
        prt_1st +=  i;
        printf("The content of the array is %c under %d position\n", *prt_1st, i);
    }
}

最佳答案

好的,您的代码中的问题是,在下面的行中

prt_1st +=  i;


它使指针增加i倍,但您需要将其增加1。

这是修改后的代码

int main()
{
    int i;
    char *prt_1st;
    char list_ch[][2] = {'1','a', '2','b', '3','c','4','d','5','e','6','f' };

    prt_1st = list_ch[0];

    for (i = 0; i < sizeof(list_ch); i++)
    {
        //prt_1st +=  i;
        printf("The content of the array is %c under %d position\n", *prt_1st, i);
        prt_1st =  prt_1st + 1;
    }
    return 0;
}

关于c - 如何在C中打印未调整大小的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35439456/

10-11 01:28