From db2a8f70e7bff800943e7a4760bd6572a8df70f6 Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Sun, 24 Jul 2016 15:46:47 -0700 Subject: [PATCH] Added optional_function_args --- .gitignore | 1 + optional_function_args/Makefile | 21 +++++++++++++++ optional_function_args/main.c | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 optional_function_args/Makefile create mode 100644 optional_function_args/main.c diff --git a/.gitignore b/.gitignore index cba7efc..6b7b389 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ a.out +test diff --git a/optional_function_args/Makefile b/optional_function_args/Makefile new file mode 100644 index 0000000..a4e41b1 --- /dev/null +++ b/optional_function_args/Makefile @@ -0,0 +1,21 @@ +#OBJS specifies which files to compile as part of the project +OBJS = main.c + +#CC specifies which compiler to use +CC = clang + +#COMPILER_FLAGS specifies the additional compilation options we're using +# -w suppress all warnings +COMPILER_FLAGS = -Wall + +#LINKER_FLAGS specifies the libraries we're linking against +LINKER_FLAGS = + +#OBJ_NAME specifies the name of our executable +OBJ_NAME= test + +#This is the target that compiles our executable +all : $(OBJS) + $(CC) $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJ_NAME) +clean : + rm test diff --git a/optional_function_args/main.c b/optional_function_args/main.c new file mode 100644 index 0000000..78ed7c7 --- /dev/null +++ b/optional_function_args/main.c @@ -0,0 +1,46 @@ +#include +#include +#include + +void example(int number_of_args, ...); + +int main() +{ + char some_var[100] = "opt1"; + char some_var2[100] = "opt2"; + char some_var3[100] = "opt3"; + + char *psome_var = some_var; + char *psome_var2 = some_var2; + char *psome_var3 = some_var3;; + + + + example(4, psome_var, psome_var2, psome_var3, psome_var); +// printf("Print array %s \n", some_var); + + return 0; +} + +void example(int number_of_args, ...) +{ + int i; + va_list curr_arg; + +// the first option tells us the number of args... so therefore opt1=numberofargs. + va_start(curr_arg, number_of_args); + + for(i = 0; i < number_of_args; i++) + { + printf("arg %d is: %s\n", i, va_arg(curr_arg, char*)); + + } + + + + /* printf("First arg: %s\n", va_arg(curr_arg, char*)); + printf("second arg: %s\n", va_arg(curr_arg, char*)); + printf("third arg: %s\n", va_arg(curr_arg, char*)); + //printf("fourth arg: %s\n", va_arg(curr_arg, char*)); */ + +}