1

I am really trying very hard to figure out how to return string from a function to other. Please help me to solve this problem.

Returning Password as String from this function:

char* password(void) { const maxPassword = 15; char password[maxPassword + 1]; int charPos = 0; char ch; printf( "\n\n\n\tPassword(max 15 Characters/no numeric value or special char is allowed):\t"); while (1) { ch = getch(); if (ch == 13) // Pressing ENTER break; else if (ch == 32 || ch == 9) //Pressing SPACE OR TAB continue; else if (ch == 8) { //Pressing BACKSPACE if (charPos > 0) { charPos--; password[charPos] = '\0'; printf("\b \b"); } } else { if (charPos < maxPassword) { password[charPos] = ch; charPos++; printf("*"); } else { printf( "You have entered more than 15 Characters First %d character will be considered", maxPassword); break; } } } //while block ends here password[charPos] = '\0'; return password; } 

To this function (but its not printing) :

void newuser(void) { int i; FILE *sname, *sid; struct newuser u1; sname = fopen("susername.txt", "w"); if (sname == NULL) { printf("ERROR! TRY AGAIN"); exit(0); } printf("\n\n\n\tYourName:(Eg.Manas)\t"); //user name input program starts here scanf("%s", &u1.UserName); for (i = 0; i < strlen(u1.UserName); i++) putc(u1.UserName[i], sname); fclose(sname); //sid=fopen("sid.txt","w"); printf("\n\n\n\tUserId:(Eg.54321)\t"); //User Id input starts here scanf("%d", &u1.UserId); printf("%s", password()); } 
3
  • 1
    You are basicly returning an address to a local variable which goes out of scope when the function ends. malloc it or make it static. Also note it is not a string literal what you are returning. Commented Jan 12, 2019 at 14:50
  • There is no string literal being returned in this code. A literal is a thing named by its value, such as 3 for 3 or "abc" for the string formed from “a”, “b”, and “c”. The thing the function attempts to return, password, is an array of char, not a string literal. Commented Jan 12, 2019 at 14:56
  • Aside: Which is easier to code/read else if (ch == ' ' || ch == '\t') or else if (ch == 32 || ch == 9) //Pressing SPACE OR TAB? Commented Jan 12, 2019 at 15:03

2 Answers 2

1

Because the lifetime of char password[maxPassword+1]; is in password function after function finished automatic delete from ram.

Variables defined inside a function, which are not declared static, are automatic. There is a keyword to explicitly declare such a variable – auto – but it is almost never used. Automatic variables (and function parameters) are usually stored on the stack. The stack is normally located using the linker. The end of the dynamic storage area is typically used for the stack.

for solving this problem you have some choices

  1. you can get this variable from argument of password function and then change it.

    void password(char password*)

  2. C dynamic memory allocation with malloc

    char *password = malloc(maxPassword+1)

    If use this method with printf("%s", password()); deliberately leaks memory by losing the pointer to the allocated memory. The leak can be said to occur as soon as the pointer 'a' goes out of scope, i.e. when function_which_allocates() returns without freeing 'a'.
    you should use free() to de-allocate the memory.

char* passwd = password(); printf("%s", passwd); free(passwd); passwd = NULL;

Sign up to request clarification or add additional context in comments.

3 Comments

The issue is the lifetime of the object, not the scope of its identifier. Lifetime is when during program execution an object exists. Scope is where in source code an identifier is visible. Objects may exist during execution of code where their identifiers are not visible.
@EricPostpischil yes,thank you.
That's one way to do it, but then he doesn't have to return anything, so it can be simply void password(char *password)
0

char password[maxPassword+1] is local to the function, you need to allocate memory for it like that char *password = malloc(maxPassword+1) or use global variable.

Also change const maxPassword=15 to int maxPassword=15, and ch=getch() to ch=getchar().

Generally I recommend reading a book about C, because it seems that you're guessing and it won't get you anywhere with C.

1 Comment

ch=getch() to ch=getchar() is a good move to standard library usage, yet OP may need getch() functionality, which differs form getchar().

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.