-1

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' 
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Muhammet Ömer
  • 145
  • 1
  • 11

1 Answers1

2
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 ?

bruno
  • 32,421
  • 7
  • 25
  • 37