Created
May 27, 2021 08:40
-
-
Save katipogluMustafa/2543abe45661f6d4fe3c98b063659098 to your computer and use it in GitHub Desktop.
Variadic arguments fail when they get less than expected number of parameters.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdarg.h> | |
int process(int param, va_list var_args) | |
{ | |
printf("\nThe First Param : %d", param); | |
char* name = va_arg(var_args, char*); | |
if(NULL != name) | |
printf(" \nThe String Param I Expect : %s", name); | |
else | |
printf("\nYou have not sent the expected second param.s ..."); | |
return 0; | |
} | |
int my_variadic_function(int first_param, ...) | |
{ | |
int ret; | |
va_list variadic_arguments; | |
va_start(variadic_arguments, first_param); | |
ret = process(first_param, variadic_arguments); | |
va_end(variadic_arguments); | |
return ret; | |
} | |
int main() | |
{ | |
my_variadic_function(22, "Mustafa"); | |
// Since the implementation expect 2 arguments, | |
// it fails if less than expected is provided | |
// Uncomment the line below to get error. | |
// my_variadic_function(22); // Segmentation fault (core dumped) | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment