请有人帮我解决这个问题,当 *next == NULL 并尝试在 Visual Studio 2010 Express 中使用错误的 ptr 调用 strlen(*next) 时,以下 C 代码中的 while 循环似乎没有退出。我已经尝试了我能想到的一切都无济于事。该代码尝试在字符串数组中查找最短和最长的字符串。
char *stringsInArray[] = {"green mats", "cat", "sat", "on", "the", "green mat", NULL};
char *shortest, *longest, **current, **next, **end;
current = stringsInArray;
next = stringsInArray + 1;
shortest = stringsInArray[0];
longest = stringsInArray[0];
while (*next != NULL)
{
if (strlen(*current) < strlen(*next))
{
if (strlen(shortest) > strlen(*next))
shortest = *next;
if (strlen(*current) < strlen(shortest))
shortest = *current;
if (strlen(*next) > strlen(longest))
longest = *next;
}
else
if (strlen(*current) > strlen(*next))
{
if (strlen(*current) > strlen(longest))
longest = *current;
if (strlen(*next) < strlen(shortest))
shortest = *next;
}
else // strlen(*stringsInArray) == strlen(*next)
{
// do nothing
}
*current++;
*next++;
}
printf("shortest string: %s\n",*shortest);
printf("longest string: %s\n",*longest);