简体   繁体   中英

Cant return a string from user input in function

I'm trying to return a string from user input in a function but apparently it doesnt work. I got the output Name: PE but thats not what I entered when using fgets.

#include <stdio.h>
#include <stdlib.h>

const char *userData();

int main() {

    char *name;
    userData();

    printf("\nName: %s", name);

}

const char *userData() {
    char *name;
    //INPUT FROM USER
    //NAME
    printf("\nEnter your name                 : ");
    fgets(name, 100, stdin);
    return name;

}

Your code has two main issues:

1.

name in main() is just a pointer.

Where do you want to store the string into?

You need to either create name as an array of char or when you use name as pointer allocate memory dynamically via a memory-management function such as malloc() where the pointer point to.

2.

How do you pass the pointer from main() to the caller? The actual parameter list of userData is empty, means nothing is passed.

You need to place char *name; in userData in the parameter list:

char *userData (char* name) {

and call userData like:

userData(name);

Note: This name now specifies the identifier of the pointer/buffer in main() , not the pointer in userData() .The fact that the pointer/buffer in the caller main() , as well as the pointer in userData() have the same name is a bad coincidence to showcase the example. But I think you will understand how it works.


#include <stdio.h>
#include <stdlib.h>

#define BUFLEN 20

char * userData (char* name, size_t len);

int main (void) {

    char* name = malloc (sizeof(char) * BUFLEN);
    if (!name)
    {
        fputs("Memory allocation for name failed!", stderr);
        return 1;
    }

    if (!userData(name, sizeof(char) * BUFLEN))
    {
        fputs("Error at string input!", stderr);
        return 1;
    }

    printf("\nName: %s", name);

}

char * userData (char *name, size_t len) {

    printf("\nEnter your name: ");

    if (!fgets(name, len, stdin))
    {
        return NULL;
    }

    return name;
}

Side Notes:

  • The return type of userData do not need to be const qualified. It has no effect. The function returns a pointer to char . The const ness has no effect as it has no influence upon whether the returned address value is accessed in the caller or not.

  • First, In C, it is not possible to pass or return strings to or from functions by value .

    Second, There is no type called " string " in C. Strings are stored in modifiable or non-modifiable arrays of char s.

    Thus, saying "returning a string" is in two contexts incorrect. Rather you pass or return a reference to the arrays containing strings by using pointers.

  • Update or swap your compiler. He is your helping hand detecting syntax errors in your code. For example GCC or Clang is to note.

  • If you have a proper compiler, never ignore compiler warnings.

The value returned from userData isn't assigned to anything in your main function. You're returning *name from the function, but you haven't told main what to do with that value it gets back. That name variable isn't shared between the functions unless you make it global, if that's what you were relying on.

The variable name is a local variable in the function userData() and its scope is limited to that function only, since the two variables are different, you won't get the result you are expecting.

Also, you should allocate memory to the char*.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM