Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        3年前关闭。
                                                                                            
                
        
如何在C中比较两位数并找到程序中每个long的交集?我有以下代码,但不确定为什么会出现段错误。我想做的是比较两个数字,看看每个数字是否相互匹配。如果这样做,则将数字设置为相同的数字,但是如果不同,则将数字设置为0。
例如,如果我有数字1 = 2421和数字2 = 2412,那么返回的数字将是2400。

   long set_intersect( long number1, long number2 )
   {
        int count=0;
        char setone[18],settwo[18];
        int final[18];
        long countone,counttwo;
        while((number1) && ( number2))
        {
                countone = number1 %10;
                setone[count] = countone;
                counttwo = number2% 10;
                settwo[count] = counttwo;
                number1 = number1/10;
                number2 = number2/10;
                count++;
        }
        for( int i= 0 ; i< count; i++)
        {
                        if( setone[i] == settwo[i])
                        {
                                final[i] = setone[i];
                        }
                        else
                        {
                                final[i] = 0;
                        }
        }
        for( i=0 ; i < count ; i++)
        {
                printf("%i\n",final[i]);
        }
}

最佳答案

由于count的未初始化值,导致行为未定义。

int count = 0;

08-15 23:02