0

I'll try be as clear as possible here but basically. (I'm doing this in c) I have an array (dynamic) of unsigned long long int's that is growing from a while loop. The workings of that are not anything fancy but I have some getter functions that give info based on what's asked.

Ie. get the first 20 numbers. this returns an unsigned long long int array of length 20 elements.

so the user will pick the number of elements on the returned array, so it could be 1 or it could be 20 large numbers (ie array[]= (1233, 123444, 1234124, 1243124, .....)

I need to get that into a string. I am putting that into a UDP packet. I am toying with sprintf but I'm no c expert so any help would be fantastic.

Thanks!

2
  • 1
    Check the return value of sprintf() for how many character printed. Commented Oct 1, 2014 at 1:22
  • What is the exact format of the desired output string? Commented Oct 1, 2014 at 1:34

1 Answer 1

2

The most efficient way would be to iteratively snprintf each element into a buffer.

Here's a function that emulates Python's join().

#include <stdio.h>

#define ARRAY_LEN(x)         (sizeof(x) / sizeof(x[0]))

/**
 * Print an array of unsigned long long integers to a string.
 * Arguments:
 *   buf      Destination buffer
 *   buflen   Length of `buf'
 *   ar       Array of numbers to print
 *   arlen    Number of elements in `ar'
 *   sep      Separator string
 * Returns:
 *   The number of bytes printed
 */
int join_ull(char *buf, int buflen,
    unsigned long long ar[], int arlen, const char *sep)
{
    int i;
    char *p;
    const char *end = buf + buflen;

    /* While we have more elements to print, and have buffer space */
    for (i=0, p=buf; i<arlen && p<end; ++i) {
        p += snprintf(p, end-p, "%llu%s", ar[i],
             (i<arlen-1) ? sep : "");        /* Print separator if not last. */

        /* Note that p is advanced to the next place we want to print */
    }

    return p-buf;
}


int main(void)
{
    unsigned long long ar[] = {1, 2, 3};
    char buf[1024];

    join_ull(buf, sizeof(buf), ar, ARRAY_LEN(ar), " ");
    printf("Output: \"%s\"\n", buf);

    /* Test the buffer-too-small case */
    join_ull(buf, 4, ar, ARRAY_LEN(ar), " ");
    printf("Output: \"%s\"\n", buf);

    return 0;
}

Result:

Output: "1 2 3"
Output: "1 2"
Sign up to request clarification or add additional context in comments.

10 Comments

elegant! just elaborate on printing the ( ) mentioned in the question
@chouaib Not sure if the OP actually wants ( ) or not.
Ha... Well that works, be it I only half understand it but I'll mess with it. Thanks you very much!
@beasts I've tried to add comments everywhere explaining what's going on. Feel free to ask if there's more I've glossed over.
If I had known about this a year ago... -_- but no, I wrote a custom lib for it because I thought you could only do it to files or the standard output.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.