Skip to content

Instantly share code, notes, and snippets.

@katipogluMustafa
Created May 27, 2021 08:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katipogluMustafa/2543abe45661f6d4fe3c98b063659098 to your computer and use it in GitHub Desktop.
Save katipogluMustafa/2543abe45661f6d4fe3c98b063659098 to your computer and use it in GitHub Desktop.
Variadic arguments fail when they get less than expected number of parameters.
#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