I have function with variable number of arguments, so I want to access them without using int variable as a number of other arguments. I was trying to access arguments using pointer to the first argument. But the result I got isn't the result I expected. You see I have special symbol \0 as an end of arguments. Any clues what to do with that? Or should I rather return to using va_list to access arguments?
#include <stdio.h>
int vowels(char symbol, ...);
int main() {
printf("%d\n", vowels('a', 'e', 'o', 'i', 'u', '\0'));
printf("%d\n", vowels('d', 'b', 'o', 'c', 'u', '\0'));
return 0;
}
int vowels(char symbol, ...) {
int counter = 0;
char *ptr = &symbol;
while (*ptr != '\0') {
if (*ptr == 'a' || *ptr == 'e' || *ptr == 'o' || *ptr == 'i' || *ptr == 'u') {
counter++;
}
ptr++;
}
return counter;
}
<stdarg.h>- do not ever try to roll this yourself.<stdarg.h>, but just had a question, thank you.