Breaking out of a loop

This is a discussion on Breaking out of a loop within the C Programming forums, part of the General Programming Boards category; I am writing a small test application that will determine what one's IP address is on a given subnet. I'm ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Breaking out of a loop

    I am writing a small test application that will determine what one's IP address is on a given subnet. I'm passing the address of a pointer from main() to getCompIPAddr(). I know that the pointer is being edited as expected once it gets to that function.

    The problem is that, in some instances, the while loop returns more than a single string containing an IP address (searches for "IPv4") and passes that to the if() block. The if() block correctly adjusts the value of the pointer but if there's more than one value, the pointer is set to whatever the last value is (usually NULL).

    What I'm not able to figure out is how to break out of the while loop when the correct value is found. I've tried variations of the code below (specifically, the if(ptr) block), but with the same result--the application hangs and never finishes running.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void getCompIPAddr(char** a);
    
    int main()
    {
        char ipAddr[16] = "000.000.000.000";
        char * pIpAddr = ipAddr;
        printf("pIpAddr before passing to getCompIPAddr(): %s\n", pIpAddr);
        getCompIPAddr(&ipAddr);
        printf("Your new main() IP address is: %s\n", *pIpAddr);
        return 0;
    }
    
    void getCompIPAddr(char** a)
    {
        FILE *fp;
        char path[1035];
    
        fp = popen("ipconfig", "r");
        if(fp == NULL) {
            printf("Failed to run command\n");
        }
        
        while(fgets(path, sizeof(path) - 1, fp) != NULL) { // Get output from command.
             //printf("%s", path);
            
            // Search substring(s)
            const char * lgString = path;
            const char * smString = "IPv4";
            char * ptr = strstr(lgString, smString);
            
        if(ptr) { // If found, filter out relevent strings by IP
                //printf("ptr: %s", ptr);
                const char * lgString2 = ptr;
                const char * smString2 = "192.168.0";
                char * ptr2 = strstr(lgString2, smString2);
                
                printf("ptr2: %s", ptr2);
                if(ptr2)
                {
                    *a = ptr2;
                    //printf("*a: %s\n", *a);
                    break;
                }
            }
        }
    }
    What is it that I'm missing that's not allowing this to work?
    Last edited by theitsmith; 02-09-2012 at 10:51 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Please don't post code pre-coloured using some IDE/tool you have.

    [COLOR=#7f4724 ]#include [COLOR=#e20000 ]<stdio.h>[/COLOR ][/COLOR ]
    [COLOR=#e20000 ][COLOR=#7f4724 ]#include [/COLOR ]<stdlib.h>[/COLOR ]
    [COLOR=#cd00a7 ]
    void[/COLOR ] getCompIPAddr([COLOR=#cd00a7 ]char[/COLOR ]** a);

    Just past as "text" in future, so we see uniformly formatted code with line numbers (fixed your post).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Because ptr2, ptr1 etc all point to your LOCAL string at line 19.
    So when the function returns, it goes out of scope, and with it your data.

    Pass in a pointer to some allocated memory, and then strcpy() what you want to keep.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Quote Originally Posted by Salem View Post
    Please don't post code pre-coloured using some IDE/tool you have.

    [COLOR=#7f4724 ]#include [COLOR=#e20000 ]<stdio.h>[/COLOR ][/COLOR ]
    [COLOR=#e20000 ][COLOR=#7f4724 ]#include [/COLOR ]<stdlib.h>[/COLOR ]
    [COLOR=#cd00a7 ]
    void[/COLOR ] getCompIPAddr([COLOR=#cd00a7 ]char[/COLOR ]** a);

    Just past as "text" in future, so we see uniformly formatted code with line numbers (fixed your post).
    Ok, apologies. I edited down the code to only the relevant pieces in an editor I don't normally use. I'll make sure it doesn't happen in the future.

    Thanks for fixing it. :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking out of a loop
    By kiros88 in forum C Programming
    Replies: 5
    Last Post: 09-01-2009, 11:49 AM
  2. Breaking out of a loop with esc key
    By bschulte in forum C Programming
    Replies: 2
    Last Post: 10-27-2008, 10:08 PM
  3. Breaking out of the loop
    By ICool in forum C Programming
    Replies: 5
    Last Post: 09-24-2007, 08:27 AM
  4. Breaking out of a loop
    By escarrina in forum C Programming
    Replies: 9
    Last Post: 04-05-2004, 12:05 PM
  5. breaking a loop
    By phantom in forum C++ Programming
    Replies: 6
    Last Post: 01-31-2003, 07:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21