tags:

views:

139

answers:

4

I have a nested for loop and I need to convert the output I get to a string. How do I do that?

e.g. my loop gives hundreds of values of numbers like:

192 168 11 248
192 168 11 249
192 168 11 250
192 168 11 251
192 168 11 252
192 168 11 253
192 168 11 254
192 168 11 255

How do I concatenate each value as 192.168.11.248 and so on.

Basically every value needs to be converted from integer to string.

A: 

You need to look into the itoa and strncat methods.

LeopardSkinPillBoxHat
-1 for `itoa` and for calling C functions "methods".
R..
+3  A: 

As this is homework, I won't post complete code, just some tips: the functions you're looking for is snprintf, the safe version of sprintf. It has the advantage that you can also do the concatenation and the dots with it.

Alternatively, you could combine itoa and strncat, but itoa is no standard function.

schnaader
+1 for `sprintf()`
Yanick Rochon
`itoa` is not a standard C function.
caf
`strtol` is a standardised function for interpreting textual representations of numbers.
dreamlax
@dreamlax: true. however, its functionality is exactly opposite to what this question needs and this answer provides
Eli Bendersky
@Eli: Yeah, I'm going to blame that one on lack of coffee. I've been making mistakes all day actually, I should probably just keep my mouth shut and wake up on the right side of the bed tomorrow.
dreamlax
A: 

If they are integers being generated you can utilize itoa and simply append that result to a character array of sufficient size with strcat.

Notice: itoa is non-standard, but it is also wide-spread and commonly used, and supplied as a non-standard library extension by many vendors. Keep in mind this means minor variations on non-standard behavior can cause significant issues in real world applications. If, however you are writing a homework assignment for one platform you will likely not encounter such issues. It is important to be aware of this and know that it won't stand up in the real world (not providing this warning was a defect in the original answer).

Other methods exist, snprintf being the chief one (with the advantage of standard behavior), but for the purpose of a simple exercise I opted to direct you to the simplest methods.

There are potential issues with some of the wide spread implementations of itoa which can crop up in edge cases, but the chance of that occurring with 3 digit positive integers is... Slim.

Regardless, go read up on snprintf as it is probably more useful in your studies of C, or if your environment supports it, consider investigating C++ and Boost as my first comment indicates a less error-prone solution in that language. C++ has the benefit of allowing for a type-safe method of specializing casts where snprintf can be easily misused (as can anything using ... and relying on a format string).

M2tM
Second link done - hope you don't mind ;)
schnaader
Thank you :) To flesh out the comment (I was 3 characters off with "thank you") I'll also mention that in C++ this could be approached with boost::lexical_cast and basic string concatenation via std::string and operator+ and there would be no risk of overflow or tricky memory allocation to account for that.
M2tM
-1 for `itoa` which is not part of the C language. Use `snprintf`.
R..
There is an implementation of itoa in "the C programming language" book. It is not a part of the standard, but I really do not understand how that is relevant to solving the problem at hand. I think you're being more pedantic than practical. I appreciate snprintf is a valid solution, but I hope you appreciate it is a little more complicated to use. I've edited my answer to reflect that itoa is non standard, but I hope you also read my comment on the C++ method of doing this and notice that boost is a "non-standard" but widely distributed library, but remains useful and practical advice.
M2tM
A: 

If you just need the output, you can do it without concatenation. Print every integer and a dot (.), except for the last integer. Pseudo code:

Print 192 "."
    Print 168 "."
        Print 11 "."
            Print 249 "\n"
Donotalo
Doesn't look like C code to me...
schnaader
@shnaader: similarly to you, the answer doesn't include real code for this obviously HW question. what's the problem?
Eli Bendersky
It is pseudo code. Indicated in the answer. Why will I give full working code?
Donotalo
@Donotalo: Something that has some hint of how it should be converted to C code would be nice. This is useless.
David Thornley