Added optional_function_args
This commit is contained in:
parent
d56cfc0a46
commit
db2a8f70e7
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
a.out
|
a.out
|
||||||
|
test
|
||||||
|
21
optional_function_args/Makefile
Normal file
21
optional_function_args/Makefile
Normal file
@ -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
|
46
optional_function_args/main.c
Normal file
46
optional_function_args/main.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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*)); */
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user