Help with C simple loop.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 33

Thread: Help with C simple loop.

  1. #1
    Join Date
    Mar 2013
    Posts
    13

    Help with C simple loop.

    Hey guys, I am trying to write a loop that will iterate through a char array in C and pull the IP addresses and test them to see if hthey respond. My ultimate goal is to have the first one that responds returned in the function, but the loop is not running correctly. It will run through right the first time, but then will start over again. Any help would be greatly appreciated. Thanks.

    Code:
    const char * getServer(char *svrList)
            {
           
     
            char *srList;
     
            srList = strtok(svrList, " ," );
            printf("Contents of srList b4 loop: %s\n", srList);
            printf("Server List: %s\n", svrList);
                    while(srList !=NULL)
                            {
                           
                            printf("result is \"%s\"\n", srList);
                            char pComm[512];
                            memset(pComm, '\0', sizeof(512));
                            sprintf(pComm, "ping -c 1 %s > /dev/null", srList);
                           
                    if((system (pComm))==0)
                            {
                            printf("result is \"%s\"\n", srList);
                            return "%s", srList;
                            }
                            else
                            {
                            printf("Hitting else loop\n");
                            printf("Contents of srList in else: %s\n", srList);
                            }
                            srList = strtok(NULL, " ," );
     
                            }
     
            return "0";
            }
     
    char svrList[] = "1.1.1.1, 2.2.2.2, 4.2.2.2, 8.8.8.8";
    getServer(svrList)

    Code output:
    Contents of srList b4 loop: 1.1.1.1
    Server List: 1.1.1.1
    result is "1.1.1.1"
    Hitting else loop
    Contents of srList in else: 2.2.2.2
    result is "2.2.2.2"
    result is "2.2.2.2"
    Contents of srList b4 loop: 1.1.1.1
    Server List: 1.1.1.1
    result is "1.1.1.1"
    Hitting else loop
    Contents of srList in else: (null)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Code:
    memset(pComm, '\0', sizeof(512));
    What does this line do?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Code:
     return "%s", srList;
    What is this supposed to do?

    The point being -- neither of the lines that I posted do what you think they do.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Mar 2013
    Posts
    13

    Re: Help with C simple loop.

    Quote Originally Posted by Paul McKenzie View Post
    Code:
     return "%s", srList;
    What is this supposed to do?

    The point being -- neither of the lines that I posted do what you think they do.

    Regards,

    Paul McKenzie
    I thought I would need this to return the first value in srList that succeeds. Is that not the case? As far as the memset function, I thought I needed this as part of iterating through the array.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Quote Originally Posted by willn View Post
    I thought I would need this to return the first value in srList that succeeds. Is that not the case?
    No. You can return only one value. I don't know where you could have gotten the information that a return statement can do all of the things you stated it can do. What you posted was usage of the comma operator, and that is what trips up a lot of people who erroneously write code that has commas.

    As far as the memset function, I thought I needed this as part of iterating through the array.
    Do you know what each of those parameters in memset() does? To the point, what does the third parameter denote? What is the value of sizeof(512)?

    http://www.cplusplus.com/reference/cstring/memset/

    Code:
    #include <stdio.h>
    int main()
    {
       printf("The sizeof(512) is %d", sizeof(512));
    }
    What gets printed?

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 8th, 2013 at 11:45 PM.

  6. #6
    Join Date
    Mar 2013
    Posts
    13

    Re: Help with C simple loop.

    Well, ideally, I was trying to return just the first address that responds and then quit the function with a return value for the IP of the server that succeeded. If it didn't, then continue the loop until all items in array are exhausted, then return "0" so that it will fail in the rest of the code. I got it to return correctly if the first IP address in the array is responding, but for some reason after it goes past the first one on a failure, it messes it all up. From what I can tell the memset will fill the char array of pComm with 0's. Not sure if that is correct.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Quote Originally Posted by willn View Post
    Well, ideally, I was trying to return just the first address that responds and then quit the function with a return value for the IP of the server that succeeded. If it didn't, then continue the loop until all items in array are exhausted, then return "0" so that it will fail in the rest of the code. I got it to return correctly if the first IP address in the array is responding, but for some reason after it goes past the first one on a failure, it messes it all up.
    You need to review the syntax of the return statement, and not guess what it does. Then write the code properly.

    The return statement is simple -- it returns a single value back to the caller, nothing else.

    From what I can tell the memset will fill the char array of pComm with 0's. Not sure if that is correct.
    Again, the third parameter is the one that is in question. It is highly important that you know what it means. That parameter tells memset() the number of bytes to set, and sizeof(512) is equal to 4 on my compiler. Do you really only want to set 4 bytes?

    You can't write a C or C++ program by guessing what various functions do, and guessing what a particular syntax is supposed to do. The reason is that it is very easy to write totally incorrect code, thinking that the code is doing its job for you. One such case is yours, and that is erroneously using the comma operator without knowing that you're using it.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 9th, 2013 at 12:02 AM.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Also, let's say you really wanted to do this:
    Code:
    return srList;
    Since srList is a pointer to a local variable, returning a pointer to a local variable is undefined behaviour. That program could have crashed as soon as you attempt to use that returned value.

    Edit: I see that you're setting it to strtok() return value, which is pointing to the string you supplied the function. So you're OK for now.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 9th, 2013 at 12:18 AM.

  9. #9
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Also, your program is not a C program. It is a C++ program that you believe is a C program. I know this, because of these lines of code:
    Code:
    printf("result is \"%s\"\n", srList);
    char pComm[512];
    In C, you cannot declare a variable after an executable statement. That statement must come first in the functional block. Only in C++ could you declare a char array at that location.

    So why are you writing C when you're obviously using a C++ compiler? If you wrote the function in C++ using std::string, then this becomes much easier.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Mar 2013
    Posts
    13

    Re: Help with C simple loop.

    OK, so in looking at the memset function again, according to this website, it would fill the array with 0's like I said, so I am stumped.http://www.elook.org/programming/c/memset.html I don't think I really NEED that memset to fix this loop, but I have looked into the return and from what I can tell, I should just reformat the return to be "return (srList);". Since you said I can only return once, if I take out the return "0", then how would I return a "0" if the loop does not contain an IP that passes the first part of the loop in the IF statement?

  11. #11
    Join Date
    Mar 2013
    Posts
    13

    Re: Help with C simple loop.

    Quote Originally Posted by Paul McKenzie View Post
    Also, your program is not a C program. It is a C++ program that you believe is a C program. I know this, because of these lines of code:
    Code:
    printf("result is \"%s\"\n", srList);
    char pComm[512];
    In C, you cannot declare a variable after an executable statement. That statement must come first in the functional block. Only in C++ could you declare a char array at that location.

    So why are you writing C when you're obviously using a C++ compiler? If you wrote the function in C++ using std::string, then this becomes much easier.

    Regards,

    Paul McKenzie
    This compiles with gcc, so I am not sure why it is letting me do it.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Another thing -- are you using the debugger that comes with your compiler to see where things break down? If you did that, then there is little need to clutter your code with printf() statements.

    First problem:
    Code:
    const char * getServer(char *svrList)
    {
        char *srList;
        srList = strtok(svrList, " ," );
        printf("Contents of srList b4 loop: %s\n", srList);
    What if srList is NULL? Your "debugging" printf() would go haywire, since it is undefined as to what will happen if you give "%s" in printf() a NULL pointer. So with that one line of code, you've potentially killed your function. That's why you should use the debugger.
    Code:
                      char pComm[512];
                      memset(pComm, '\0', sizeof(512));
    This should be
    Code:
                      char pComm[512];
                      memset(pComm, '\0', sizeof(pComm));
    Next:
    Code:
           return "%s", srList;
    The only thing returned here is srList, all of the result of the comma operator.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Quote Originally Posted by willn View Post
    OK, so in looking at the memset function again, according to this website, it would fill the array with 0's like I said, so I am stumped.
    Did you read what each parameter does? What does the third parameter denote?

    When you call a function, you're supposed to not only know what the function does, but make sure you know what each parameter is supposed to denote. You're stuck on what the function does, and I'm trying to get you to read the docs on what each parameter does. If you did that, you should see that the last parameter that you provided to memset() is not correct:
    num
    Number of bytes to be set to the value.
    size_t is an unsigned integral type.
    The array is 512 bytes, but you're telling memset() to set sizeof(512) bytes, which is equal to 4.
    Since you said I can only return once,
    No. What I stated is that you can only return one value. You can have a thousand return statements, but each one only returns one value.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 9th, 2013 at 12:43 AM.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,446

    Re: Help with C simple loop.

    Quote Originally Posted by willn View Post
    This compiles with gcc, so I am not sure why it is letting me do it.
    You need to know if you are compiling a C++ program. If you are, then the syntax is valid. If not, then I don't know if this is one of gcc's "extensions", or if it is something in C99 (I don't use C99) that makes it compile.

    One way to know for sure is to include a C++ header such as <string> and see if the code still compiles. If it still compiles, then you're compiling a C++ program and not a C program.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Mar 2013
    Posts
    13

    Re: Help with C simple loop.

    OK, I am sorry, I see what you are saying. I am in the process of debugging, but since I am in windows, I cannot find a good debugger that will work well. I have visual studio, but it does not include all the classes that I am using in my program and from what I read it is not recommended to add them. Any suggestions on this?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center