954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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

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

char getString(char)

int main() {
    char string[200];
    string = getString(string);
}

char getString(char string) {
    scanf("%s",string);
    return string;
}
Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
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.

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:

char *getString( char *string, int length ) {
  char format[ 30 ];
  sprintf( format, "%%%ds", length );
  scanf( format, string );
  return string;
  }

Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

Study the interface which fgets() provides.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

char *getString(char *string);

int main() {
    char string[200];
    getString(string);
}

char *getString(char *string) {
    scanf("%200s",string);
    return string;
}

Ahh, thanks. Worked like a charm. Exactly what I needed.

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 
> 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,

char *getString(char *string) {
	scanf("%199s",string);
	return string;
}


to provide for the null char. Thanks again:)

Barefootsanders
Junior Poster
166 posts since Oct 2006
Reputation Points: 10
Solved Threads: 3
 

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

char *getString(char *string) {
	scanf("%199s",string);
	return string;
}

to provide for the null char. Thanks again:)


We still recommend:

char *getString(char *string) {
	<strong>fgets(string, 200,stdio);</strong>
	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.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Yet still, the size of the buffer which is being written to isn't being passed as a parameter.
*shrug*

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You