Thread: working with strings arrays and pointers

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    37

    Unhappy working with strings arrays and pointers

    I'm working on another program, which creates sentences from randomly chosen words.... I just started and have this little part of code, and it just crashes the whole thing... Cannot find the reason why....
    If anybody can tell me why, I'd be really grateful.
    Code:
    int main()
    {
    
    	char noun[] =  "bank cat mouse car lady";
    	char verb[] =  "wetted kissed swum walked skipped";
    	char preposition[] = "to from over under on";
    	char article[] = "the a one some any";
                    char  *current_noun;
    	char *current_verb;
    	char *current_prep;
    	char *current_art;
    	int i = 1;
    	int number = 0;
    	long ltime = 0;  
    	unsigned stime = 0;
    
    noun_ptr1[0] = current_noun = noun;
    verb_ptr[0] = current_verb = verb;
    prep_ptr[0] = current_prep = preposition;
    art_ptr[0] = current_art = article;
    
    
    while ( (current_noun = strchr(current_noun, ' ')) != NULL)
    {
    	*current_noun++ = '\0';
    	noun_ptr1[i++] = current_noun;
    }
    
    
    
    printf("%s, %s, %s, %s, %s\n", noun_ptr1[1], noun_ptr1[2], noun_ptr1[3], noun_ptr1[4], noun_ptr1[5]);
    
    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    noun_ptr1[0] = current_noun = noun;
    verb_ptr[0] = current_verb = verb;
    prep_ptr[0] = current_prep = preposition;
    art_ptr[0] = current_art = article;

    These variables are not defined anywhere. Your code doesn't have a 'noun_ptr1', verb_ptr, etc etc.

    Fix that first.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    Sorry, I thought I copied everything...
    What about now?
    Code:
    int main()
    {
    
    	char noun[] =  "bank cat mouse car lady";
    	char verb[] =  "wetted kissed swum walked skipped";
    	char preposition[] = "to from over under on";
    	char article[] = "the a one some any";
    	char *noun_ptr1[35];
    	char *verb_ptr[35];
    	char *prep_ptr[35];
    	char *art_ptr[35];
        char *current_noun;
    	char *current_verb;
    	char *current_prep;
    	char *current_art;
    	int i = 1;
    	int number = 0;
    	long ltime = 0;  
    	unsigned stime = 0;
    
    noun_ptr1[0] = current_noun = noun;
    verb_ptr[0] = current_verb = verb;
    prep_ptr[0] = current_prep = preposition;
    art_ptr[0] = current_art = article;
    
    
    while ( (current_noun = strchr(current_noun, ' ')) != NULL)
    {
    	*current_noun++ = '\0';
    	noun_ptr1[i++] = current_noun;
    }
    
    
    
    printf("%s, %s, %s, %s, %s\n", noun_ptr1[1], noun_ptr1[2], noun_ptr1[3], noun_ptr1[4], noun_ptr1[5]);
    
    return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In your printf() statement, noun_ptr1[5] is unitialised.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    37
    OOOOO! Now I noticed! Thanks .... Something as small as a blank space here and there creates so many problems...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 11:25 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  5. pointer to pointers to arrays of strings??
    By Binkstone in forum C Programming
    Replies: 8
    Last Post: 09-14-2001, 02:56 AM