2

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.

Matthew Schell
  • 599
  • 1
  • 6
  • 19
  • Possible duplicate in [How can I read an input string of unknown length?](https://stackoverflow.com/questions/16870485/how-can-i-read-an-input-string-of-unknown-length) and many others to be found. – Weather Vane Nov 15 '20 at 17:36
  • You need to allocate memory, for that you need a function or can be on you main, that receives the data from user and realloc memory for that data – Gonçalo Bastos Nov 15 '20 at 17:43
  • @Matthew Schell check my example! Its working for me – Gonçalo Bastos Nov 15 '20 at 19:00
  • Thanks @GonçaloBastos, but what exactly is the program you typed actually doing? It looks very confusing – Matthew Schell Nov 15 '20 at 19:04
  • @MatthewSchell Its your program but use dynamic arrays! It will change the size of array until gets the size of the string putted by user! Do you use linux, to programming? – Gonçalo Bastos Nov 15 '20 at 19:07
  • @MatthewSchell I just do what you want! The variables are type char* and I allocate memory, for the string size! Its working for me! – Gonçalo Bastos Nov 15 '20 at 19:13
  • @MatthewSchell Leave me feedback if works – Gonçalo Bastos Nov 15 '20 at 19:20
  • @GonçaloBastos Thank you so much for helping me! I use a mac for programming. I'm about to paste the code in my IDE. – Matthew Schell Nov 15 '20 at 19:27
  • @GonçaloBastos I am looking in my IDE and it appears that I don't have debug.h do you know how to install that? I've looked on the internet but found nothing – Matthew Schell Nov 15 '20 at 19:31
  • @MatthewSchell I use a makefile to work on it, but you dont need to include them! And I dont know if it works on mac, what program you use? – Gonçalo Bastos Nov 15 '20 at 19:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224598/discussion-between-matthew-schell-and-goncalo-bastos). – Matthew Schell Nov 15 '20 at 19:35

1 Answers1

1

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

Gonçalo Bastos
  • 376
  • 3
  • 16