You are not logged in.

#1 2013-04-07 10:47:45

Boogie
Member
Registered: 2012-06-17
Posts: 27

[SOLVED] stdarg.h and va_list. [C]

I want to build a function to strdup each element of a char **string.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>


char **new_string_vector(const char *x, ...) {
        va_list ap;
        const char *s;
        char **return_string;
        unsigned i = 0, n = 0;

        va_start(ap, x);
        while ((s = va_arg(ap, const char*)))
                n++;

        return_string = malloc(n+1 * sizeof(char*));
        return_string[0] = strdup(x);

        while ((s = va_arg(ap, const char*)))
                return_string[i++] = strdup(s);

        va_end(ap);
        return return_string;
}

void free_string_vector(char **l) {
        char **c;
        for (c = l; *c; c++) {
                free(*c);
        }
        free(l);
}

int main(int argc, const char *argv[])
{
        char **iter;
        char **teststring = new_string_vector("blah", "bla", "fdsa");
        for (iter = teststring; iter && *iter; iter++)
                printf("%s\n", *iter);

        free_string_vector(teststring);

        return 0;
} 

I cant seem to figure out what is goin wrong hmm im new to this va_list.

Last edited by Boogie (2013-04-08 12:10:30)

Offline

#2 2013-04-07 12:16:20

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] stdarg.h and va_list. [C]

You can't go over the va_list twice. Call your function something like this:

new_string_vector("one", "two", "three", NULL);

And check for the NULL in your while loop.

Offline

#3 2013-04-07 12:23:29

Trilby
Forum Moderator
From: Massachusetts, USA
Registered: 2011-11-29
Posts: 13,831
Website

Re: [SOLVED] stdarg.h and va_list. [C]

Or just go through once and use realloc for the char ** rather than malloc.

char **new_string_list(const char *x,...) {
	va_list ap;
	const char *s;
	char **return_string;
	int n =0;
	va_start(ap, x);
	while ((s = va_arg(ap, const char*))) {
		return_string = (char **) realloc(return_string,(n+1)*sizeof(char *));
		return_string[n++] = strdup(s);
	}
	va_end(ap);
	return return_string;
}

Last edited by Trilby (2013-04-07 13:20:22)


InterrobangSlider
• How's my coding? See this page.
• How's my moderating? Feel free to email any concerns, complaints, or objections.

Offline

#4 2013-04-08 12:09:49

Boogie
Member
Registered: 2012-06-17
Posts: 27

Re: [SOLVED] stdarg.h and va_list. [C]

Thanks for input! I made it with this approach:

char **new_str_list(const char *y, ...) {
        va_list ap, aq;
        unsigned n = 1, i = 0;
        char **return_str;
        const char *x;

        va_start(ap, y);
        va_copy(aq, ap);
        while ((x = va_arg(aq, const char*)))
                n++;
        va_end(aq);

        return_str = calloc(n+1, sizeof(char*));
        if (!return_str)
                return NULL;

        return_str[i] = strdup(y);

        while ((x = va_arg(ap, const char*)))
                return_str[++i] = strdup(x);

        va_end(ap);

        return return_str;
}

Offline

Board footer

Powered by FluxBB