I think the code and error is self-explanatory. Why is this problem arising? Intuitively speaking, I think it has something to do with using pointer arrays instead of regular pointers. Other than that I'm lost!
Code and compiler output below
Code:
root[~]# cc -Wall -pedantic 8.11.c
8.11.c: In function 'main':
8.11.c:32: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
8.11.c:32: warning: format '%s' expects type 'char *', but argument 3 has type 'int'
8.11.c:32: warning: format '%s' expects type 'char *', but argument 4 has type 'int'
8.11.c:32: warning: format '%s' expects type 'char *', but argument 5 has type 'int'
8.11.c:32: warning: format '%s' expects type 'char *', but argument 6 has type 'int'
8.11.c:32: warning: format '%s' expects type 'char *', but argument 7 has type 'int'
root[~]# ./a.out
Segmentation fault
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main()
{
char *article[ 5 ] = { "the", "a", "one", "some", "any" };
char *noun[ 5 ] = { "boy", "girl", "dog", "town", "car" };
char *verb[ 5 ] = { "drove", "jumped", "ran", "walked", "skipped" };
char *preposition[ 5 ] = { "to", "from", "over", "under", "on" };
srand( time( NULL ) );
printf( "%s %s %s %s %s %s\n",
*article[ rand() % 5 + 1 ],
*noun[ rand() % 5 + 1 ],
*verb[ rand() % 5 + 1 ],
*preposition[ rand() % 5 + 1 ],
*article[ rand() % 5 + 1 ],
*noun[ rand() % 5 + 1 ] );
return 0;
}