Pointer troubles

This is a discussion on Pointer troubles within the C Programming forums, part of the General Programming Boards category; Hi! I'm doing exercises with pointer; in order to create a random sentence generator I programmed an array of pointer ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15

    Pointer troubles

    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

    ???

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    printf("%s ", (*words_index[ word_sequence[lil_cntr] ]) +  (dice(DIM) -1) )
    Where does this add on to?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    The output of the program should come as no surprise to you because you're randomizing the starting location in the array of strings.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21