0

I need to pass variable argument to another function. The problem is that this argument is an function and i have no idea how to determine the type of funtion. For example i have function: char(*)char(c), how to read it by va_arg?

void process(const char* typ ...)
{
    char c;
    va_list ap;
    
    va_start(ap, typ);

    while (*typ != 0)
    {
        switch (*typ)
        {
            case'p':
                //change(va_arg(ap, char*), va_arg(ap, char(*)char(c)));
            case 'f':
                //filterr(va_arg(ap, char*), va_arg(ap, bool(*)char));
            case 's':
                char* a = va_arg(ap, char*);
                char* b = va_arg(ap, char*);
                encrypt(a, b);
                break;
        }
    }
}

char changeToUppear(char c) {
    return (c >= 'a' and c <= 'z') ? c - 32 : c;
}
bool leaveLowercase(char c) {
    return (c < 'a' or c>'z');
}


9
  • Some actual code would help. Have you ever used varargs before? Do you have a good reference on how they work? Commented Dec 19, 2020 at 12:02
  • Welcome to Stack Overflow. Please read how to ask good questions. Make sure your question covers these 3 elements: 1. Problem Statement 2. Your Code (it should be Minimal, Reproducible Example) 3. Error Message (preferably full Traceback to help others review and provide feedback). Sometimes the same question may have already been asked. Make sure your question is not a duplicate. Commented Dec 19, 2020 at 12:05
  • I have never used them before. I read quite alot and I had a lesson about them. Commented Dec 19, 2020 at 12:18
  • You can use a va_list as an argument to a function. Take a look at vprintf() and its siblings, for example. Commented Dec 19, 2020 at 12:29
  • 2
    Why do you want to use varargs here? What's the advantage of the monolithic whopper process("pfs",s1,changeToUppear,s2,leaveLowercase,s3,"TAJNE"); over the more usual map(s1, toUpper); filter(s2, isLower); encrypt(s3, "TAJNE");, which gives you the benefit of type safety?` Commented Dec 19, 2020 at 12:35

1 Answer 1

1

char(*)char(c), how to read it by va_arg?

Just take it, like any other type. The function type is char (*)(char). After correcting many typos in your code, the following compiles:

#include <iso646.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
void change(char (*func)(char), char *pnt);
char filterr(bool (*func)(char), char *pnt);
void encrypt(char *, char*);

void process(const char* typ, ...) {
//                          ^ !!
    char c;
    va_list ap;   
    va_start(ap, typ);
    while (*typ != 0) {
        switch (*typ) {
            case'p': {
                char *pnt = va_arg(ap, char*);
                char (*func)(char) = va_arg(ap, char (*)(char));
                change(pnt, func);
            }
                break;
            case 'f': {
                char *pnt = va_arg(ap, char*);
                bool (*func)(char) = va_arg(ap, bool (*)(char));
                filterr(pnt, func);
            }
                break;
            case 's': {
                char* a = va_arg(ap, char*); // it's not possible to declare variable after case!
                char* b = va_arg(ap, char*);
                encrypt(a, b);
            }
                break;
        }
    }
    va_end(ap); // remember about va_end!
}

char changeToUppear(char c) {
    // prefer toupper from ctype.h
    return (c >= 'a' and c <= 'z') ? c - 32 : c;
}
bool leaveLowercase(char c) {
    // prefer islower() != 0 from ctype.h
    return (c < 'a' or c>'z');
}

int main() 
{
    char s1[] = "Ala ma kota.";
    char s2[] = "Kot ma Ale!";
    char s3[] = "to jest bardzo tajny tekst";
    process("pfs",s1,changeToUppear,s2,leaveLowercase,s3,"TAJNE");
}
Sign up to request clarification or add additional context in comments.

Comments

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.