If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Help with C simple loop.

Another important point if this is really a 'C' program:

You should post a full program, including headers that you included. The reason is that a C program will and can behave differently if headers are missing. If you post code with no headers (as you did in your initial post), then there is no telling how the program will behave.

Re: Help with C simple loop.

Originally Posted by willn

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?

Visual Studio has one of, if not, the most powerful debuggers out there. People debug all sorts of programs, from student programs to full-fledged systems of all types. So I don't know what you mean by "all the classes you're using".

I will cautiously say that you're not using the Visual Studio debugger correctly.

This is the full code for the client side. It works except for the getservers function. May not be the best as far as structure, but I just need to do certain things. The visual studio was giving me cannot open source file for the following:
#include <socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

I am assuming this means that these are classes that are used mainly in Linux and not windows.

Re: Help with C simple loop.

Originally Posted by willn

I am assuming this means that these are classes that are used mainly in Linux and not windows.

Most of those headers are non-standard, more precisely the ones that deal with sockets. If you want to use sockets on both Windows and Linux, then you need to get a cross-platform solution, otherwise you have to write two different sets of code for Linux and Windows.

Secondly, sockets just confuse the issue. The problem is that you cannot take a string that is separated by commas, and produce each field -- in other words, a CSV parser. It doesn't matter what the data is going to be used for. If you wrote a simple function to do this first, then you can use any compiler to test the code since no sockets are involved. Instead, you're trying to learn basic 'C' coding within a larger program, and it's all of that unnecessary noise that this socket code is causing that is confusing the issue.

The first thing to realize is that strtok() messes up your original char array by placing NULLs in the data. Do you want that to happen? If not, then you need to copy the data to a temporary array and use that instead. The other thing is that strtok() can only be used in a "serial" way, i.e. you can't have two or more places where strtok() is being called interchangeably with different strings, where one strtok() is being called during the processing of the other strtok(). This is all due to strtok() using static variables.

Also, your code still doesn't suggest that it is really C that you're using. I still see variables being declared after executable statements, like this:

Code:

printf("Please enter a floating point number: ");
float number2;

If you tried to compile this with Visual Studio as a 'C' module, it will give you an error stating that you can't declare a float at that point in the code. Visual C++ follows the rules of ANSI 'C', and if you turned on the ANSI 'C' switch for gcc (that is if this is really and truly a 'C' program), you will get the same error using gcc. The only way to not get an error for this construct is if the code is C++, and so far, you haven't confirmed what language you're really compiling for.

If it is really C++, this job becomes much easier and safer.

Finally, you need to properly indent your code. It is almost impossible to follow with the indentation all over the place like that.

Re: Help with C simple loop.

So, the function is working like I want it to. When I run it by itself or output the result of the function to a printf, it has the correct return value. Where the problem comes in is when I call the function a second time. It seems like after this, something is different and it doesn't run the same the second time. I am referring to the code snippet that I added in my main program:

Checking which server to use.
Function return is: 129.120.151.95:
There are no available servers!

Which if you look at the main program, it is returning the "0" value on the second time the function is run, but the correct value the first time. Am I missing something in the way it is running the function?

Re: Help with C simple loop.

More than likely you mean
return 0;
or even better, return NULL; (same thing, just more clear)

When you test it here,
if (getServer(svrList) != "0")
you're actually comparing pointers, not the value they point to, so they'll never be equal. Return 0 and test for 0, not "0".

I changed the return to a NULL in the function and that line 'if (getServer(svrList) != "0")' to 'if (getServer(svrList) != NULL)' and it made no difference on the output. I do thank you for helping me with a better way of returning from the function. That helps me write a little better code. I know I am completely green right now.

Re: Help with C simple loop.

You can't compare c-type NULL terminated strings like this. To compare these type of strings, you use

Code:

if (!strcmp(getServer(srvList) , "0"))

A better way as GCDEF said would be for getServer to return NULL instead of returning "0".

You also need to note, as said in previous posts, that strtok alters the contents of the string on which it is used. So if you call it more than once on the same string it will produce different answers! As said previously, copy the string first before using strtok. This is why calling getSever with the same argument more than once produces different results!

All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
C, C++ Compiler: Microsoft VS2015

Re: Help with C simple loop.

Originally Posted by Paul McKenzie

The first thing to realize is that strtok() messes up your original char array by placing NULLs in the data. Do you want that to happen? If not, then you need to copy the data to a temporary array and use that instead. The other thing is that strtok() can only be used in a "serial" way, i.e. you can't have two or more places where strtok() is being called interchangeably with different strings, where one strtok() is being called during the processing of the other strtok(). This is all due to strtok() using static variables.

Also, your code still doesn't suggest that it is really C that you're using. I still see variables being declared after executable statements, like this:

Code:

printf("Please enter a floating point number: ");
float number2;

If you tried to compile this with Visual Studio as a 'C' module, it will give you an error stating that you can't declare a float at that point in the code. Visual C++ follows the rules of ANSI 'C', and if you turned on the ANSI 'C' switch for gcc (that is if this is really and truly a 'C' program), you will get the same error using gcc. The only way to not get an error for this construct is if the code is C++, and so far, you haven't confirmed what language you're really compiling for.

If it is really C++, this job becomes much easier and safer.

Finally, you need to properly indent your code. It is almost impossible to follow with the indentation all over the place like that.

Regards,

Paul McKenzie

What can I use instead of strtok() because I will need to call this function multiple times to make sure that the server is still up and if not, move to the next one in the list, so dealing with a function that modifies my array is not good? I have verified in codelite that the string is being modified (not that I didn't believe you guys, just cool to see it for myself in the debugger). Also, as far as C versus C++, the requirement for this program is C, so I am kinda stuck on that one. I am still not sure why the code compiles with the way it is structured, but as long as it works, I can clean it up later. Just wanted to say I really appreciate the responses I have gotten on this, you guys are freakin awesome!!

Re: Help with C simple loop.

Originally Posted by willn

What can I use instead of strtok() because I will need to call this function multiple times to make sure that the server is still up and if not, move to the next one in the list, so dealing with a function that modifies my array is not good?

There is no out-of-the-box 'C' function to parse without altering the string. You need to write it yourself or get one off the Net somewhere.

Also, as far as C versus C++, the requirement for this program is C, so I am kinda stuck on that one.

The problem is that what you wrote violates C rules, so it isn't really a C program.

I am still not sure why the code compiles with the way it is structured,

Please check all of your gcc command-line options. One of them is more than likely the ANSI switch that turns on ANSI C compilation. Once you compile with that option, you will see that the code I pointed out will fail to compile. I'm sure gcc has this option, as there still is a lot of 'C' code out there that requires ANSI 'C' compatibility without any extensions or C99 support.

Re: Help with C simple loop.

I think I finally got it!!! Thanks to you guys' help! The answer was the strcpy() function. One other question though, why can't I use a dynamic array with strcpy()? In the final code, I get an error if I use char *temp; but it works with char[512] as a static array. Couldn't find an explanation for that one. The explanation of strcpy says to prevent overflows, the size of destination has to be long enough to contain the same C string as source, but that doesn't explain why a dynamic array wouldn't work, because I would assume it should allocate it accordingly. That may be my wondering mind thinking without knowing how it works.... Also, do I have to have the exact array size as the source also or is it bad practice to add a little bit for "cushion"?

Re: Help with C simple loop.

One other question though, why can't I use a dynamic array with strcpy()?

There is no such thing as a dynamic array in 'C'. All arrays are fixed in size.

In the final code, I get an error if I use char *temp; but it works with char[512] as a static array.

There is a big difference between a pointer and an array. A pointer must point somewhere valid before you read or write to where it's pointing. Did you do that?

The explanation of strcpy says to prevent overflows, the size of destination has to be long enough to contain the same C string as source, but that doesn't explain why a dynamic array wouldn't work, because I would assume it should allocate it accordingly.

C has none of these "automatic" facilities to resize strings or arrays. Arrays are fixed in size. The closest you can get is by using the heap (malloc(), calloc(), realloc(), etc.). However, once you go down that route, then you better know what you're doing.

That's why I asked whether you are really compiling a C++ module (that looks like it's C). With C++, you do have the facilities to resize strings and arrays using the various container and string classes.

Also, do I have to have the exact array size as the source also or is it bad practice to add a little bit for "cushion"?

If you overflow the boundaries of the array, the result is undefined behaviour. Even if you're off by one byte, the program is flawed.

So yes, you should leave yourself more room, but that is not the greatest of solutions. The best solution would be to write a library of dynamic string routines for 'C', and use them instead of raw char* pointers and arrays. However, that is an undertaking for someone who knows 'C' fairly well and knows what to look out for.