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: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

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

 
0
  #1
Nov 15th, 2007
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

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

 
0
  #2
Nov 15th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
1
  #3
Nov 15th, 2007
Study the interface which fgets() provides.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

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

 
0
  #4
Nov 15th, 2007
Originally Posted by Duoas View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #5
Nov 15th, 2007
> 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 164
Reputation: Barefootsanders is an unknown quantity at this point 
Solved Threads: 3
Barefootsanders Barefootsanders is offline Offline
Junior Poster

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

 
0
  #6
Nov 15th, 2007
Originally Posted by Salem View Post
> 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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

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

 
0
  #7
Nov 15th, 2007
Originally Posted by Barefootsanders View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #8
Nov 16th, 2007
Yet still, the size of the buffer which is being written to isn't being passed as a parameter.
*shrug*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC