Thread: Question about char arrays

  1. #1
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728

    Question about char arrays

    I've only used strings in my programs but for class I was instructed to use character arrays which I am completely unfamiliar with. Basically I need to create random sentences by pulling verbs, nouns, etc from an array of char arrays. For some reason though, if I pull the last word in the array I get gibberish. Like for example in the line return articles[rand()%5] if the random number is 4, my sentence prints random characters for the word, even though there does exist a word at array location 4. The only way I've been able to solve it is to extend the arrays to size six instead of five. Can someone explain to me why I get gibberish when I try to return the last word in the array? MSVC++ by the way.

    Code:
    #include<cstring>
    #include<iostream>
    #include<ctime>
    
    using namespace std;
    
    void printSentence(void);
    
    char* returnArticle(void);
    char* returnNoun(void);
    char* returnVerb(void);
    char* returnPreposition(void);
    
    
    int main()
    {
    	srand(time(NULL));
    
    	for (int x=0; x<20; x++)
    	{
    		printSentence();
    	}
    
    	return 0;
    }
    
    void printSentence()
    {
    	// create the sentence by pulling random verbs, articles, prepositions, and nouns
    	char sentence[50]="";
    	char period[2]=".";
    	char space[2]=" ";
    	strcat(sentence, returnArticle());
    	strcat(sentence, space);
    	strcat(sentence, returnNoun());
    	strcat(sentence, space);
    	strcat(sentence, returnVerb());
    	strcat(sentence, space);
    	strcat(sentence, returnPreposition());
    	strcat(sentence, space);
    	strcat(sentence, returnArticle());
    	strcat(sentence, space);
    	strcat(sentence, returnNoun());
    	strcat(sentence, period);
    
    	sentence[0]=toupper(sentence[0]);
    
    	cout<<sentence<<endl;
    }
    
    char* returnArticle()
    {
    	char articles[5][5]={"the", "a", "one", "some", "any"};
    	return articles[rand()%5];
    }
    
    char* returnNoun()
    {
    	char nouns[5][5]={"boy", "girl", "dog", "town", "car"};
    	
    	return nouns[rand()%5];
    }
    
    char* returnVerb()
    {
    	char verbs[5][8]={"drove", "jumped", "ran", "walked", "skipped"};
    
    	return verbs[rand()%5];
    }
    char* returnPreposition()
    {
    	char prepositions[5][6]={"to", "from", "over", "under", "on"};
    
    	return prepositions[rand()%5];
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    you are returning a pointer to a variable that is local to that function. when that function dies, that variable dies with it, but the pointer still points to where the variable used to be.

    the only reason it seems to work most of the time is that most of that array still exists in its normal state after the function dies (memory doesnt get reset after variables die, its just ignored). when its messing up is when something overwrites that area of memory partially, so you get whatever is in the memory pointed at by that pointer, which now looks like gibberish.

    there are a couple ways you can solve it. you can make all of the character arrays in the functions static so they dont die, or you can create the variables outside of the function and pass them in. how you do it is up to you.
    Last edited by ...; 10-20-2003 at 04:02 PM.
    I came up with a cool phrase to put down here, but i forgot it...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    char *articles[5]={"the", "a", "one", "some", "any"};
    Should fix that problem
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    youre still returning a pointer to invalid memory. it may work most of the time, but its still going to cause problems occasionally.
    I came up with a cool phrase to put down here, but i forgot it...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    return "hello";
    is not returning a pointer to invalid memory. String constants have global scope - the fact that you have an array of them makes no difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Though your compiler won't complain if you leave it out, it's really one of the things that might help you when you least expect it:

    char* returnArticle(void);

    should be

    const char* returnArticle(void);

    because you are returning a pointer to a string constant.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. char ** and argv memory question
    By simo_mon in forum C Programming
    Replies: 3
    Last Post: 08-17-2008, 08:47 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Question on char arrays
    By homeyg in forum C++ Programming
    Replies: 22
    Last Post: 12-23-2004, 04:45 PM
  5. Newb Question - Cant convert Char[41] to Char
    By Kalkran in forum C++ Programming
    Replies: 2
    Last Post: 08-19-2004, 10:05 AM