How can I select a random element from a character array in c ?
For instance:
char *array[19];
array[0] = "Hi";
array[1] = "Hello";
etc
I am looking for something like array[rand], where rand is the random integer number between o and the array's length(in this case 20) like 1, 2, 3 , 19 etc.
rand()char* array[20];20 * rand()is not correct. Userand() % 20. However, if your array is 20 elements long you need to fix your variable declaration! You're only allocating 19 elements there.%because the results can be non-uniform. Multiplying a U(0,1) value times 20 and flooring will give an int from 0 to 19, i.e., 20 elements all set for zero-based array indices.