Search code examples
carraysrandomindexingvariadic

Print the array index in variadic functions


I want to print the index in the array with random. But I can't.

Main.c:

RastgeleKarakter randomB = overload5_specifiedRandom(6,'g','y','u','c','n','e');

RastgeleKarakter.c

RastgeleKarakter overload5_specifiedRandom(int args,...){
  va_list list;
  va_start(list, args);
  char array[7];
  char* test;
  int sayi = (int) ((Now())%args);
  for(int i = 1; i <= args; i++){
    array[args] = (char) va_arg(list,int);
    printf("%c ", array[args]);
  } 
  printf("%d",sayi);
  va_end(list);
}

Out:

g y u c n e 3╝

I want this out :

'u' or 'g' or 'c' 

Solution

  • char array[7];
      ...
    for(int i = 1; i <= args; i++){
      array[args] = (char) va_arg(list,int);
      printf("%c ", array[args]);
    

    if there are too much argument you go out of the array with an unexpected behavior

    very probably you wanted array[i] rather than array[args], else there is no reason at all to have an array and it can be replaced by char c;

    Out:

    g y u c n e 3╝

    I want this out :

    'u' or 'g' or 'c'

    there is no test to write or not (char) va_arg(list,int); (forgetting the probable problem on the index) so how do you hope to not write all of them ?