| | |
Function syntax to take in a char array and return keyboard input
Thread Solved |
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
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
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
C Syntax (Toggle Plain Text)
char getString(char) int main() { char string[200]; string = getString(string); } char getString(char string) { scanf("%s",string); return string; }
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.
You can pass the length of the target string into the function if you want:
Hope this helps.
Also, don't use %s in scanf() without qualifying it with a maximum width.
C Syntax (Toggle Plain Text)
char *getString(char *string); int main() { char string[200]; getString(string); } char *getString(char *string) { scanf("%200s",string); return string; }
You can pass the length of the target string into the function if you want:
C Syntax (Toggle Plain Text)
char *getString( char *string, int length ) { char format[ 30 ]; sprintf( format, "%%%ds", length ); scanf( format, string ); return string; }
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
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.
C Syntax (Toggle Plain Text)
char *getString(char *string); int main() { char string[200]; getString(string); } char *getString(char *string) { scanf("%200s",string); return string; }
•
•
Join Date: Oct 2006
Posts: 164
Reputation:
Solved Threads: 3
•
•
•
•
> 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.
C Syntax (Toggle Plain Text)
char *getString(char *string) { scanf("%199s",string); return string; }
to provide for the null char. Thanks again
•
•
•
•
sorry, I should have said I changed it to this,
C Syntax (Toggle Plain Text)
char *getString(char *string) { scanf("%199s",string); return string; }
to provide for the null char. Thanks again
char *getString(char *string) {
fgets(string, 200,stdio);
return string;
}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. The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
- One line of code copy char array using pointers (C++)
- sending address of multidimensional char array to a function (C)
- Handling keyboard input (C++)
- Return a 2D array from a function (C++)
- convert char* to double (C++)
- school project: simple C++ game (need help with keyboard input) (C++)
- Keyboard Input (Java)
- Singly-Linked Lists: Ultimate (C++)
- How to assign my own numeric values to a char array? (C++)
Other Threads in the C Forum
- Previous Thread: representing binary values
- Next Thread: Conways game of life in C
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches include infiniteloop initialization input intmain() iso keyboard km license linked linkedlist linux list looping loopinsideloop. lowest matrix microsoft mysql oddnumber open opendocumentformat openwebfoundation pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test threads unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






