| | |
Function syntax to take in a char array and return keyboard input
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
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
Views: 2648 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for C
#include * array arrays asterisks binarysearch calculate changingto char cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv fflush file fork forloop framework functions getlasterror givemetehcodez grade graphics gtkwinlinux hacking hardware histogram homework inches include incrementoperators input iso kernel keyboard km lazy linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft motherboard mqqueue mysql number opendocumentformat opensource overwrite owf pattern pdf performance pointer pointers posix problem probleminc process program programming radix recursion recv research reversing scanf scripting segmentationfault sequential socket socketprograming spoonfeeding standard string structures student systemcall testing threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windowsapi






