Hi!
I'm doing exercises with pointer; in order to create a random sentence generator I programmed an array of pointer to pointer .... to char.
But when i try to print the string randomly, several parts of them are truncated. i don't see why
Here is the code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIM 5
#define WI_DIM 4
#define WS_DIM 6
int main(void)
{
unsigned char lil_cntr; /* a little counter */
char *article[DIM] = {"the", "a", "one", "some", "any"};
char *noun[DIM] = {"boy", "girl", "dog", "town", "car"};
char *verb[DIM] = {"drove", "jumped", "ran", "walked", "skipped"};
char *preposition[DIM] = {"to", "from", "over", "under", "on"};
char ** words_index[WI_DIM] = {article, noun, verb, preposition};
/* to have a sentence we use in sequence article, noun, verb,
preposition, article and noun: under here you find that
sequence in terms of words_index indexes */
int word_sequence[WS_DIM] = {0,1,2,3,0,1};
/* sentence composition */
for (lil_cntr=0; lil_cntr <WS_DIM ; ++lil_cntr){
printf("%s ", (*words_index[ word_sequence[lil_cntr] ]) + (dice(DIM) -1) );
}
printf("\n");
return 0;
}
int dice(int faces )
{
static i = 1;
if(i){
srand(time(NULL));
i--;
}
return (1 + rand() % faces);
}
E.g.
Code:
luca@eee:~$ ./a.out
e boy ve rom the girl
luca@eee:~$ ./a.out
a girl rove o e boy
luca@eee:~$ ./a.out
a girl ove to the boy
luca@eee:~$ ./a.out
a ve from y
luca@eee:~$ ./a.out
boy ve boy
???