So I am trying to create my first program, and I got stuck. Basically, what I am trying to do is to create an array which includes strings/chars and integers, and from that I would like the program to randomly print any of its liking out.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int *test[] = { 1, 2, 3, 444, 5, 'Hi', 7 };
int i;
int j;
for (j = 0; j < 7; j++) {
printf("%d\n", rand(*test));
}
return(0);
}
For some reason this isn't working properly, because I get the output of 22997
, 17678
, etc.
My second problem is the other part of my code. I'm trying to print the whole array, and I want it to be printed out as both characters and integers.
for (i = 0; i < 7; i++) {
printf("\n%d\n", test[i]);
}
Here I get all the integers, but the string "Hi"
I get in ASCII value. Is there some way that you can include both integers and characters in an array and print it out in the same loop? Also, how is it that I can just use a short string in this array, but I can't use something like "HelloTherefriend"
?
Would appreciate some clarity here as it would help me a lot!