Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

va_list with multiple types

Name: Anonymous 2010-07-24 1:23

i want to write soemthing like this:
a function which will print various types of arguments.
I tried to work with va_list, it doesn't work.
int main(){
write("this ",1.2," is ",'a',7," test");
}
should result in:
this 1.2 is a7 test

Name: Anonymous 2010-07-24 1:30

lol

Name: Anonymous 2010-07-24 1:39

learn2printf

#include <stdio.h>
int main(){ printf("%s%.1f%s%c%d%s\n", "this ", 1.2, " is ", 'a', 7, " test"); }

Name: Anonymous 2010-07-24 1:59

>>3
this isn't a multiple type function, your types are fixed.
"%s%.1f%s%c%d%s\n" specifies number&type of arguments.
Can you write it without "%s%.1f%s%c%d%s\n"?

Name: Anonymous 2010-07-24 2:07

>>4
you can use whatever types you want, you just have to pass the types to the function somehow. that's the standard way to accomplish that.

Name: Anonymous 2010-07-24 2:08

>>5
What if you don't know the types?

Name: Anonymous 2010-07-24 2:18

>>6
Then you're an incompetent idiot and you should stay the fuck away from anything more complicated than a light switch.

Name: Anonymous 2010-07-24 2:18

What i want :
functionname (...){for(i=0;i<args;i++)print(eachArgument[i]);}
float a=5.4;char* b="text";int c=1;long double d=5e200;
functionname(a,b,c,d)
functionname(a,b,c)
functionname(a,b)
Is this possible in C?

Name: Anonymous 2010-07-24 2:21

>>8
Why are you trying to force a toy language to support things it was not meant to be?

Name: Anonymous 2010-07-24 2:35

>>8
yes, if print is just void print(uintmax_t n){ printf("%" PRIuMAX "\n", n); }.
if you want type-specific behavior, you have to tell the function what types you're giving it. there's already an established way to do that, which >>3 showed you.
or, you could do this:
struct idiot_object{
  enum type { INT, FLOAT, STR };
  union value { uintmax_t i;
                unsigned char c;
                long double f;
                char *s; }}
...
struct idiot_object a = { .type = FLOAT, .value = { .f = 5.4 }},
                    b = { .type = STR,   .value = { .s = "text" }},
                    c = { .type = INT,   .value = { .i = 1 }},
                    d = { .type = FLOAT, .value = { .f = 5e200 }};

Name: Anonymous 2010-07-24 2:50

>>10
What if the arguments are not a fixed structure? i.e. random number of argument and random type of arguments.

Name: Anonymous 2010-07-24 2:57

>>11
There is no way to check the type of the arguments at the runtime in C. Use some real language instead.

Name: Anonymous 2010-07-24 3:01

>>12
Only if by "real" you mean "toy". RTTI is never needed, and imposes a huge performance penalty.

>>11
Then you either add more types to the struct or just use [code]printf[code]-style format strings to pass the type information to the function.

Name: Anonymous 2010-07-24 3:14

Name: Anonymous 2010-07-24 3:21

Have you seen Boost.Assign?

vec += 1, 2, 3;
it does that by overloading the comma operator.

Name: Anonymous 2010-07-24 3:26

>>15
>Overloading the comma operator
Is there nothing sacred?

Name: Anonymous 2010-07-24 4:36

If C does not store RTTI why is there a need for casting from type to type(except BDSM)?

Name: Anonymous 2010-07-24 4:45

>>17
* compile-time type checking (can detect a lot of common programmer errors)
* converting between types with different binary representations

Name: Anonymous 2010-07-24 7:06

retards and c don't mix

Name: Anonymous 2010-07-24 8:10

>>19
Oh yes they do. All too often, sadly. :-(

Name: Anonymous 2010-07-24 8:24

C - easy to learn, hard to master.

Name: Anonymous 2010-07-24 8:44

>>21
Easy to think you can make it do something that is never necessary if you can use the correct tools.

Name: Anonymous 2010-07-24 11:19

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

struct idiot_object
{
  enum { INT, FLOAT, STR, CHAR } type;
  union
    {
        int i;
        char c;
        float f;
        char *s;
    } value;
};


void print(struct idiot_object *first, ...);
void print_idiot(struct idiot_object *);

void print_idiot(struct idiot_object *hi)
{
    switch(hi->type){
        case INT:   printf("%d", hi->value.i); break;
        case FLOAT: printf("%2.2f", hi->value.f); break;
        case STR:   printf("%s", hi->value.s); break;
        case CHAR:  printf("%c", hi->value.c); break;
        default:    puts("your an idiot"); break;
    }
}

void print(struct idiot_object *first, ...)
{
    va_list l;

    print_idiot(first);

    va_start(l, first);

    while((first = va_arg(l, struct idiot_object *)))
        print_idiot(first);

    va_end(l);
}

int main(void)
{
    struct idiot_object a = { .type = FLOAT, .value = { .f = 5.4 }},
                                            b = { .type = STR,   .value = { .s = "text" }},
                                            c = { .type = INT,   .value = { .i = 1 }},
                                            d = { .type = FLOAT, .value = { .f = 5e3 }},
                                            e = { .type = STR,   .value = { .s = "\n" }};


    print(&a, &b, &c, &d, &e, NULL);

    return 0;
}


tl;dr: OP you're an idiot.

Name: Anonymous 2010-07-24 11:34

>>23
U MENA   union
    {
        int i;
        char c;
        float f;
        char *s;
    };

Name: Anonymous 2010-07-24 15:32

>>15
overloading the comma operator
I lol'd

Name: Anonymous 2010-07-24 16:37

http://www.boost.org/doc/libs/1_43_0/libs/assign/doc/index.html
It's a tragicomedy.
Maybe if they hadn't succumbed to Bjarne's corruption at a young age, they could have gone on to create some great TeX packages.

Name: Anonymous 2010-07-24 18:38

array of const

Good old Object Pascal.

Name: Anonymous 2010-07-24 18:51

>>1
Learn2use the Printf method also check out what formatting signs (don't know the actual word for it in English) you need to use.
I don't remember... %d is for ints... I think.

Name: Anonymous 2010-07-24 18:54

>>28
lrn2readthefuckingthreadandsage

Name: Anonymous 2010-07-24 18:54

>>29
Oh right, I forgot that... Sorry, I've been gone for two weeks.

Name: Anonymous 2010-07-24 19:02

wow this was quite a while ago!
hella sad!
its all in the love of fun!
http://www.youtube.com/watch?v=128IR21ZQa0

Name: Anonymous 2010-07-24 21:26

OP, what you want is a different programming language. Don't try to force C to be that language, just use that language, whatever it may be.

Name: Anonymous 2010-07-24 21:32

>>26
I haven't laughed this hard since Rosencrantz and Guildenstern Are Dead!

Name: Anonymous 2010-11-14 23:19

Name: Anonymous 2010-11-26 21:53

Name: Anonymous 2011-02-03 3:56


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List