![]() |
| ||
| Function syntax to take in a char array and return keyboard input Hey everyone, I'm working on function that will take in a char array, prompt the user to input a string, and then return that string to the function. Below is what I've been working on and yes, I know it is not the right syntax, but I'm hoping someone could help me fix it so it works. Thanks! -Barefoot char getString(char) |
| ||
| Re: Function syntax to take in a char array and return keyboard input You are on the right track. The only problem is that char is a single character, not a whole list of them (or array of them). You probably want to use a pointer to an array of chars. Also, don't use %s in scanf() without qualifying it with a maximum width. char *getString(char *string); You can pass the length of the target string into the function if you want: char *getString( char *string, int length ) {Hope this helps. |
| ||
| Re: Function syntax to take in a char array and return keyboard input Study the interface which fgets() provides. |
| ||
| Re: Function syntax to take in a char array and return keyboard input Quote:
|
| ||
| Re: Function syntax to take in a char array and return keyboard input > Worked like a charm. Exactly what I needed. That's a shame, considering that there's a bug in the code. Let's see if other people can spot it. |
| ||
| Re: Function syntax to take in a char array and return keyboard input Quote:
char *getString(char *string) {to provide for the null char. Thanks again:) |
| ||
| Re: Function syntax to take in a char array and return keyboard input Quote:
char *getString(char *string) {It's just too easy to make a mistake with scanf(). And why do you need to pull in the and execute the parsing for %d, %u, %i, %g, etc when fgets()requires nothing extra but reading a string. Safely. |
| ||
| Re: Function syntax to take in a char array and return keyboard input Yet still, the size of the buffer which is being written to isn't being passed as a parameter. *shrug* |
| All times are GMT -4. The time now is 4:58 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC