Thread: Trouble passing string arrays through functions..

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    3

    Trouble passing string arrays through functions..

    Hi everyone,

    I'm very new to C. I've dabbled in some Java, C#, etc...

    I'm simply trying to create random sentences using strings. For instance...:

    Code:
    char getNoun() {	
    	int randNum = (rand() % 5);
    
    
    	char nounOne[] = "boy";
    	char nounTwo[] = "girl";
    	char nounThree[] = "dog";
    	char nounFour[] = "town";
    	char nounFive[] = "car";
    	//printf("%d", randNum); for debugging
    	switch(randNum) {
    		case 0: //printf("%s", nounOne);
    			return nounOne[0];
    			break;
    		case 1: //printf("%s", nounTwo);
    			return nounTwo[0];
    			break;
    		case 2: //printf("%s", nounThree);
    			return nounThree[0];
    			break;
    		case 3: //printf("%s", nounFour);
    			return nounFour[0];
    			break;
    		case 4: //printf("%s", nounFive);
    			return nounFive[0];
    			break;
    		default: return 'p';  break;
    	
    	}
    
    
    }
    
    
    int main()
    {
    	srand((unsigned)time(NULL));
    
    
    	printf("%s %s\n", getNoun(), getNoun());
    
    
    	system("pause");
    
    
        return 0;
    }
    It works when I printf as "%s" in the getNoun function...but when I call the getNoun() function and try to print it as a "%s" my program crashes. If I printf it as "%c" it will print the first character....

    So... I'm very new to pointers and references.... Am I anywhere close to where I need to be? Do I need to iterate through the characters in the string array somehow using pointers?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Like this
    Code:
    char *getNoun() {
      char *nouns[] = {
        "boy",
        "girl",
      };
      return nouns[rand()%2];
    }
    You can't have
    char nounOne[] = "boy";
    because nounOne is a local variable, and strings are not returned as arrays, but as pointers.
    So when the array goes out of scope, the pointer becomes invalid.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays and string arrays to functions
    By codeer in forum C Programming
    Replies: 2
    Last Post: 03-25-2015, 10:07 AM
  2. Replies: 3
    Last Post: 02-04-2013, 04:28 PM
  3. Trouble passing args to functions in other files
    By Midnight Coder in forum C Programming
    Replies: 6
    Last Post: 01-03-2009, 05:13 PM
  4. More trouble passing arrays
    By Rad_Turnip in forum C Programming
    Replies: 2
    Last Post: 04-04-2006, 08:11 PM
  5. Having Trouble Passing typedef Arrays to a Function
    By jlharrison in forum C Programming
    Replies: 1
    Last Post: 03-27-2006, 12:06 PM

Tags for this Thread