| | |
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 |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






