Reputation: 11
I have troubles getting this operation done. I actually want to create a function that returns a simple word from a scanf command, to get it called into main code. I know that functions cant return arrays properly, but II have seen lots of tutorial and don't understand it yet. For university purposes.
#include <stdio.h>
#include <cstring>
int function() {
char word[100];
scanf("%s", &word);
return word;
}
int main() {
char word2[100] = function();
}
Upvotes: 1
Views: 3876
Reputation: 123578
So, the main problem is that functions cannot return array types, and you cannot copy the contents of one array to another using the =
operator. Arrays in C are different from other types.
Except when it is the operand of the sizeof
or unary &
operators, or a string literal used to initialize a character array in a declaration, an expression of type “N-element array of T
” is converted (“decays”) to an expression of type “pointer to T
” and the value of the expression is the address of the first element. So when you write
return word;
it’s being interpreted as
return &word[0]; // return char *
Since word
is local to function, it ceases to exist when function
exits, so you wind up returning an invalid pointer.
There are three ways around this:
Declare word
as static
:
char *function( void )
{
static char word[100];
scanf( "%s", word ); // no & operator here
return word; // return char *
}
Declaring the array as static
means it exists over the lifetime of the program, not just that function. This works, but it limits the function’s utility, and is not recommended.
Allocate the array dynamically:
char *function( void )
{
char *word = malloc( sizeof *word * 100 );
scanf( "%s", word ); // again, no & operator
return word;
}
This is better than making the array static
, but now you have to make sure you free
the array when you are finished with it.
Pass the destination array as an argument to the function:
void function( char *word, size_t size )
{
scanf( "%s", word );
return word;
}
This is my preferred method, but in this particular case you might as well just call scanf
directly.
So that covers the function side, let’s tackle the caller side.
The only valid initializers for an array declaration are either a brace-enclosed sequence of values:
char word[100] = {'H', 'e', 'l', 'l', 'o', 0 };
or, in the case of character arrays, a quoted string:
char word[100] = "Hello";
You cannot initialize an array in a declaration with the result of a function, because a function cannot return array types.
To copy the contents of one array to another, you need to use either strcpy
(for strings, which are arrays of char
with a terminating 0
) or memcpy
(for all other array types), and you can’t do that in a declaration.
If all you need to do is read a word into the array, just bypass function
and call scanf
directly:
int main( void )
{
char word[100];
scanf( “%s”, word );
// do stuff with word
}
Upvotes: 2
Reputation: 3699
In your code, int function()
, the return value of this function is int
but you want to get the string, so use char * function()
instead.
And, scanf("%s",&word);
do not use &
for array of character.
The example for your code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char * function () {
char * word = malloc(sizeof(64));
if (!word)
return NULL;
scanf("%s",word);
return word;
}
int main () {
char * word2 = function();
printf("%s\n", word2);
free(word2);
return 0;
}
Upvotes: 2