while (*next != NULL) loop not exiting when pointer is NULL

Go To StackoverFlow.com

3

Please can someone help me with this problem, the while loop in the following C code doesn't seem to exit when *next == NULL and tries to call strlen(*next) with a bad ptr in Visual Studio 2010 Express. I've tried everything I can think of to no avail. The code tries to find the shortest and longest strings in an array of strings.

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);
2012-04-05 19:04
by Alastair
Why aren't you using a for loop - Dunes 2012-04-05 19:07
The value of *next never reaches NULL, but you assumed it did. Check your assumptions - Greg Hewgill 2012-04-05 19:09


4

You ought to change

*current++;
*next++;

to

current++;
next++;

Both increment ok, but the former also dereference / return value of current and next; which is not needed.

Your problem, however is this:

printf("shortest string: %s\n", *shortest);
printf("longest string: %s\n",  *longest);

Here you try to print a character using string as format.

This should work:

printf("shortest string: %s\n", shortest);
printf("longest string: %s\n",  longest);
2012-04-05 19:29
by Morpfh
@PaulR: please re-count the levels of indirection.. - Christoph 2012-04-05 19:34
@Christoph: my bad - Paul R 2012-04-05 19:36
@PaulR that is not the problem the code works as it should, it just doesn't exit the while loop when the last string has been read. I have tried a for loop and get the same problem. Could it be that a bad ptr is not the same as a nice clean null ptr and Visual Studio doesn't evaluate the condition *next != NULL to true for a bad ptr - Alastair 2012-04-05 19:45
Thanks guys for your help. I don't know why I was having trouble with it. May be it had something to do with compiling in debug mode in Visual studio 2010 - Alastair 2012-04-05 20:01
@Alastair: Probably because you have changed something else, while at same time fixing a bug you wasn't aware of, changed the logic, or etc. I see you have i.e. **end which not is in use. Perhaps this one was part of the fail. - Anyhow: I'm on linux and not able to test VS. But take notice and read warnings carefully (should appear at bottom of VS-gui). Also increase warning level to max - Morpfh 2012-04-05 20:17


0

The code actually works as expected - you just need to change

printf("shortest string: %s\n",*shortest);
printf("longest string: %s\n",*longest);

to

printf("shortest string: %s\n",shortest);
printf("longest string: %s\n",longest);
2012-04-05 19:30
by Christoph
@PaulR: did you actually compile the code? works for me.. - Christoph 2012-04-05 19:35
Yes, I tried it with gcc -O0 and gcc -O3 and got different results - there may be another problem that I've missed though - have now deleted my answer and comments - Paul R 2012-04-05 19:37


-1

%s expects a pointer to char i.e., char *. Hence it has to be shortest and longest which are pointers instead of *shortest and *longest which are the values at pointer locations

2015-01-24 00:07
by Saisujithreddy
I already know how pointers work, but this is still confusing to read - Nathan Tuggy 2015-01-24 00:29
I just mean that we need to use a pointer for %s format specifier. *shortest or *longest are not pointers which is causing the code not to work. shortest and longest are the pointers - Saisujithreddy 2015-01-24 22:16
Ads