944,148 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3405
  • C RSS
Nov 15th, 2007
0

Function syntax to take in a char array and return keyboard input

Expand Post »
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

  1. char getString(char)
  2.  
  3. int main() {
  4. char string[200];
  5. string = getString(string);
  6. }
  7.  
  8. char getString(char string) {
  9. scanf("%s",string);
  10. return string;
  11. }
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Nov 15th, 2007
0

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.

  1. char *getString(char *string);
  2.  
  3. int main() {
  4. char string[200];
  5. getString(string);
  6. }
  7.  
  8. char *getString(char *string) {
  9. scanf("%200s",string);
  10. return string;
  11. }

You can pass the length of the target string into the function if you want:
  1. char *getString( char *string, int length ) {
  2. char format[ 30 ];
  3. sprintf( format, "%%%ds", length );
  4. scanf( format, string );
  5. return string;
  6. }
Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 15th, 2007
1

Re: Function syntax to take in a char array and return keyboard input

Study the interface which fgets() provides.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 15th, 2007
0

Re: Function syntax to take in a char array and return keyboard input

Click to Expand / Collapse  Quote originally posted by Duoas ...
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.

  1. char *getString(char *string);
  2.  
  3. int main() {
  4. char string[200];
  5. getString(string);
  6. }
  7.  
  8. char *getString(char *string) {
  9. scanf("%200s",string);
  10. return string;
  11. }
Ahh, thanks. Worked like a charm. Exactly what I needed.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Nov 15th, 2007
0

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 15th, 2007
0

Re: Function syntax to take in a char array and return keyboard input

Click to Expand / Collapse  Quote originally posted by Salem ...
> 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.
sorry, I should have said I changed it to this,

  1. char *getString(char *string) {
  2. scanf("%199s",string);
  3. return string;
  4. }

to provide for the null char. Thanks again
Reputation Points: 10
Solved Threads: 3
Junior Poster
Barefootsanders is offline Offline
165 posts
since Oct 2006
Nov 15th, 2007
0

Re: Function syntax to take in a char array and return keyboard input

sorry, I should have said I changed it to this,

  1. char *getString(char *string) {
  2. scanf("%199s",string);
  3. return string;
  4. }

to provide for the null char. Thanks again
We still recommend:
char *getString(char *string) {
	fgets(string, 200,stdio);
	return 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.
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 16th, 2007
0

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*
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: representing binary values
Next Thread in C Forum Timeline: Conways game of life in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC