我不明白的問題(目前還不清楚你想達到什麼)......
正如其他人已經說過,沒有邊界的陣列,這是不對的檢查...
這裏是你的代碼的一些其他反饋:
// func1 - consider giving functions a meaningful name, it helps people to
// understand what the function is supposed to be doing....
// In this instance, it might have been helpful to identify what the expected
// return values/inputs of the function are...
int func1(int *str)
{
int i;
// Start a counter at 0, loop (adding 1) while
// the current value of the counter is less than, the value held in the
// array so, {1,2,3,4,0,7} Would terminate on the 0
// This: {1,20,7,14,0,7} Would also terminate on the 0
// This seems wrong, but again, it's unclear what you're trying to do here.
for(i=0;i<*(str+i);i++) {
// If the current element of the array
// is the same as the next element of the array
if(*(str+i) == *(str+i+1))
{
// return 1 - two numbers next to each other in the
// array are the same?
return 1;
}
}
// Either: The array contained a digit less than the counter,
// Or: It didn't contain two numbers that were the same next to each other.
// This seems a bit wrong?!?
return 0;
}
你的問題可以改進(以獲得更多有用的答案),如果你表現出你期待什麼樣的輸入返回什麼返回值。
基於此'我將需要編寫一個函數,如果它在數組成員之間發現差異,將返回true。
在僞代碼,好像你會想:
// Loop, checking we don't overflow. No point checking the last element as
// there's nothing after it to check...
for (count = 0 to arraysize -1) {
// If the current element != the next element, we've found a difference?!?
if(arrayElement[count] != arrayElement[count+1) {
return true
}
}
return false
UPDATE:
在新的代碼...
// You're still assuming the size of 'str'
int func1(int *str)
{
int i,temp=0;
// Loop while i < 9, i.e. 9 times.
for(i=0;i<10-1;i++) {
if(*(str+i) == *(str+i+1))
{
temp++;
// Temp can never == 10, you're only going round the loop 9 times...
// Maybe it should be (temp == 10-1), but I don't know where the
// 10 comes from...
if(temp == 10)
{
return 1;
}
}
}
return 0;
}
此:
if(*(str+i) == *(str+i+1))
{
temp++;
// Temp can never == 10, you're only going round the loop 9 times...
if(temp == 10)
{
return 1;
}
}
可能是:
// return 0 (FALSE) on first difference
if(*(str+i) != *(str+i+1))
{
return 0;
}
如果您在您的函數結束時return 1
改變
return 0
這是功課? – 2011-05-25 07:50:34如果你更喜歡它,你可以使用數組符號:'str [i + 1]'和'*(str + i + 1)'在上面的代碼中完全一樣。 – pmg 2011-05-25 07:57:28
這是一個保證數組是否被排序以開始?如果不是,你需要比鄰居多。 – pmg 2011-05-25 07:58:59