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?