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()); }
mallocit or make itstatic. Also note it is not a string literal what you are returning.3for 3 or"abc"for the string formed from “a”, “b”, and “c”. The thing the function attempts to return,password, is an array ofchar, not a string literal.else if (ch == ' ' || ch == '\t')orelse if (ch == 32 || ch == 9) //Pressing SPACE OR TAB?