How to store a string that the user inputted into a char pointer instead of a char array

Viewed 67

I'm trying to build a program with C but I'm having trouble changing a char array into a char pointer and adjusting the program accordingly. Here's my current code that I want to change:

#include <stdio.h>
#include <string.h>
#include <unistd.h>

char username[20];
char password[20];
char username_input[20];
char password_input[20];
char user_input;

void create_account(char* usrname, char* passwd) {
    printf("==================================CREATE BANK ACCOUNT==================================\n");
    while(1) {
        printf("Enter a username that is less than 20 characters: ");
        scanf("%s", usrname);

        if (strlen(usrname) <= 20)
            break;

        printf("That is not less than 20 characters, try again...\n");
        sleep(2);
    }

    while(1) {
        printf("Enter a password that is less than 20 characters: ");
        scanf("%s", passwd);

        if (strlen(passwd) <= 20) {
            break;
        }
        printf("That is not less than 20 characters, try again... \n");
        sleep(2);
    }

    printf("Thank you, please sign in now...\n");
    sleep(2);
}

void login() {
    while(1) {
        printf("Enter Username: ");
        scanf("%s", username_input);
        printf("Enter Password: ");
        scanf("%s", password_input);
        if (strcmp(username, username_input) != 0 || strcmp(password, password_input) != 0) {
            printf("Incorrect Username or Password. Try again...\n");
            sleep(2);
        }

        else {
            printf("Welcome %s\n", username);
            sleep(2);
            break;
        }
    }
}

On the lines at the beginning, you can see that there are 4 char array declarations. I want them to be char pointers like so:

char* username;
char* password;
char* username_input;
char* password_input;

The reason for this is because I don't want a limit in a string, but arrays need limits. Once I change that, I want to use malloc() to allocate memory for what the user inputs but I don't know how. In other words, I want to declare a char pointer that accepts user input. And I want enough memory to be allocated for that pointer so that the string that was inputted has enough space. Also I want my code to be compatible with different compilers and computers. For that I'm pretty sure that I have to multiply the malloc() function with sizeof(char) or something like that. I don't necessarily get an error, as in I don't get red lines in my IDE, but the program stops in the middle of it for no reason and gives me an exit code other than 0.

1 Answers

I have done something like this to alloc memory:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

void create_account(char** usrname, char** passwd);
void login(char** usrname, char** passwd, char** input_usrname, char** input_passwd);
void AllocMemory(char*** buf);

int main(){
    char* username;
    char* password;
    char* username_input;
    char* password_input;

    create_account(&username, &password);
    login(&username, &password, &username_input, &password_input);
    free(username);
    free(password);
    free(username_input);
    free(password_input);
    return 0;
}

void create_account(char** usrname, char** passwd) {
    printf("==================================CREATE BANK ACCOUNT==================================\n");
        printf("Enter a Username: ");
        AllocMemory(&usrname);
        printf("Enter a Password: ");
        AllocMemory(&passwd);
    printf("Thank you, please sign in now...\n");
    sleep(2);
}

void login(char** usrname, char** passwd, char** input_usrname, char** input_passwd) {
    while(1) {
        printf("Enter Username: ");
        AllocMemory(&input_usrname);
        printf("Enter Password: ");
        AllocMemory(&input_passwd);
        if (strcmp(*usrname, *input_usrname) != 0 || strcmp(*passwd, *input_passwd) != 0) {
            printf("Incorrect Username or Password. Try again...\n");
            sleep(2);
        }

        else {
            printf("Welcome %s\n", *usrname);
            sleep(2);
            break;
        }
    }
}

void AllocMemory(char*** buf){
    int bufSize = 10;
    int stringSize;
        
    **buf = calloc(bufSize, sizeof(char)); 
        
    if(**buf == NULL){
        printf("[ERROR] can't malloc %d bytes\n", bufSize);
        exit(1);
    }
    char *readpos = **buf; //point to a pointer of your array!
        
    while(1){   //looping until the alocated memory is enough to the inserted command

        do{

            fgets(readpos, bufSize, stdin); //reads a line from the specified stream

            stringSize = strlen(**buf); //getting the size of the array

            if (stringSize == 1)
            {
                printf("\nYou just pressed enter, pls type again: "); //checking if user just pressed enter
            }

        }while (stringSize == 1); //looping until user press only enter

    
        if (readpos[strlen(readpos)-1] == '\n'){ //Search from the end as there's where the newline should be if exists, the string fits on array and doesnt need to allocate more memory
                readpos[strlen(readpos)-1] = '\0';     //Remove \n from the string
                break;
        }   

        
        **buf = realloc(**buf, bufSize + stringSize * sizeof(char)); // Need to allocate more memory, because the before if its false
        if(*buf == NULL){
            printf("[ERROR] can't realloc more %d bytes\n", bufSize+1);
            exit(1);
        } 

        readpos = **buf + stringSize; // Set the pointer to next position to read into 
    }
        
}

I dont know if you are using global variables, but this dont have global variables! But if you want you can use them

Related