DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Function syntax to take in a char array and return keyboard input (http://www.daniweb.com/forums/thread96847.html)

Barefootsanders Nov 15th, 2007 8:26 am
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;
}

Duoas Nov 15th, 2007 10:05 am
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.

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.

Salem Nov 15th, 2007 10:05 am
Re: Function syntax to take in a char array and return keyboard input
 
Study the interface which fgets() provides.

Barefootsanders Nov 15th, 2007 6:07 pm
Re: Function syntax to take in a char array and return keyboard input
 
Quote:

Originally Posted by Duoas (Post 470380)
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.

Salem Nov 15th, 2007 7:08 pm
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.

Barefootsanders Nov 15th, 2007 7:20 pm
Re: Function syntax to take in a char array and return keyboard input
 
Quote:

Originally Posted by Salem (Post 470764)
> 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:)

WaltP Nov 15th, 2007 9:41 pm
Re: Function syntax to take in a char array and return keyboard input
 
Quote:

Originally Posted by Barefootsanders (Post 470768)
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) {
        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.

Salem Nov 16th, 2007 4:19 am
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*


All times are GMT -4. The time now is 4:58 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC