使用递归函数在数组中搜索值

时间:2018-07-18 20:16:50

标签: c

有人可以向我解释为什么这行不通吗?我已经搞砸了很多次,但我似乎无法做到,因此它实际上可以搜索。知道,它跳过了函数的递归部分。

Option Explicit

Sub sendFromOtherMailbox()

    ' Code in Outlook, not called from another application

    Dim objMailMessage As mailItem
    Dim uMailbox As recipient

    ' Should not be necessary but this is used later to ensure
    ' the entry in .SentOnBehalfOfName is what you think it is.
    Set uMailbox = Session.CreateRecipient("Preferably display name here rather than email address.")

    uMailbox.Resolve

    ' An email address always resolves so the "If Resolved" test is not useful
    '  if an email address was used in .CreateRecipient
    If uMailbox.Resolved Then

        Set objMailMessage = CreateItem(olMailItem)

        With objMailMessage

            .Subject = "send From Other Mailbox"
            .SentOnBehalfOfName = uMailbox
            .Display

        End With

    End If

End Sub

编辑:谢谢所有帮助的人,我意识到当我尝试调试时,我将跳过该功能而不是再次进入该功能。现在正在工作!

1 个答案:

答案 0 :(得分:2)

有些事情与您的代码不正确。首先是您在7函数的size参数中使用了Search

您似乎想从最后一个索引开始线性搜索数组,然后一直向下搜索到索引0,因此您需要进行检查,否则您的函数将继续按其方式工作向下堆栈内存,直到发生分段错误,或者您最终遇到恰好在其中存储value的内存。

这是您可以执行的许多修复程序之一,以使该功能达到您的期望。我将使用0来表示未能找到您想要的值:

int Search(int *tab, int tam, int valor);

int main(int argc, char** argv) {

    int index;
    int tab[5] = {12, 25, 3, 14, 18};

    index = Search(tab, 5, 3); // Changed 7 to 5 to reflect the size of the array.

    if(index == 1)
        printf("You found the value!!\n\n");
    else
        printf("NO LUCK!\n\n");
}

int Search(int *tab, int size, int value) {

    if (size <= 0) // We've searched all values in the array
        return 0;
    else if (tab[size - 1] == value) // Array indexing starts at 0, so need to subtract 1 here.
        return 1;
    else
        return Search(tab, size - 1, value); 

}