Question

I am having problems trying to copy the contents of a char array into an array of char pointers in C. My code is listed below:

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

# define RECORD_SIZE 300
# define BUFFER_SIZE 3

/* Structure for representing a single
 * password entry */
typedef struct Record {
    char * sitename;
    char * username;
    char * password;
    struct Record * next;
} Record;

/* Declare function prototypes */
int isCorrectKey(char *,char *);
int isValidOperation(char);
int isValidSyntax(char *);
void getFields(char *,char * []);
void listEntries();
int updateEntries(char *);
int deleteEntries(char *);
int entryExists(char *);
void init(char *,char *,char *);
void readDB();
void writeToDB(char *);
void encrypt(char *,char * [],char *);
void decrypt(char *,char * [],char *);

/* Declare global variables */
char * database;
Record * records;

// Pre-condition: A character pointer to plaintext
// A character pointer to an empty buffer,
// A character pointer to the user input key
// Post-condition: The plaintext is encrypted and
// inserted into the buffer
void encrypt(char * text,char * buffer[BUFFER_SIZE],char * key) {
    int i = 0;

    for(;i < strlen(text);i++) {
        buffer[i] = ((char *) (text[i]+3));
    }
    buffer[i] = 0;
}

// Pre-condition: A character pointer to ciphertext
// A character pointer to an empty buffer
// A character pointer tothe user input key
// Post-condition: The ciphertext is decrypted and
// inserted into the buffer
void decrypt(char * text,char * buffer[BUFFER_SIZE],char * key) {
    int i = 0;

    for(;i < strlen(text);i++) {
        buffer[i] = ((char *) (text[i]-3));
    }
    buffer[i] = 0;
}

// Pre-condition: The database variable must be set
// The records variable must exist
// Post-condition: The value in database variable is
// used as filename to read the data. The data that
// is read and used to initialize the records variable
void readDB() {
    // Open the file in read-only mode
    FILE * f = fopen(database,"r");
    char buffer[BUFFER_SIZE];
    int index = 0;

    if(f == NULL) {
        // Print error if file is invalid
        printf("Sorry. Unable to find password database.");
    } else {
        char c = 0;
        // Read the file
        while((c = fgetc(f)) != EOF) {
            if(c != '\n') {
                buffer[index++] = c;
            } else {
                buffer[index] = 0; // terminate each entry with null
                index = 0;
            }
        }
        fclose(f); // Close the file handle
    }
}

// Pre-condition: The database variable must be set
// A character pointer to some text must be provided
// as input
// Post-condition: The value in database variable is
// used as filename to write the data. 
void writeToDB(char * text) {
    // Open the file in append mode
    FILE * f = fopen(database,"a");
    int index = 0;

    if(f == NULL) {
        // Print error if file is invalid
        printf("Sorry. Unable to find password database.");
    } else {
        fputs(text,f); // Write to the file
        fclose(f); // Close the file handle
    }
}

// Pre-condition: A character pointer to the user input key
// A character pointer to the actual key
// The records variable must be set
// Post-condition: Returns 1 if the key value in the records variable
// has been properly decrypted.
// Returns 0 otherwise.
int isCorrectKey(char * uKey,char * mkey) {
    return strcmp("ThisIsTheSecretKey",records->password);
}

// Pre-condition: A character indicating an operation value
// Post-condition: Returns 1 if the operation value is supported
// Returns 0 otherwise.
int isValidOperation(char operation) {
    return operation == 'L' || operation == 'U' || operation == 'D';
}

// Pre-condition: A character pointer to a user input command string
// Post-condition: Returns 1 if the syntax of the command is correct
int isValidSyntax(char * command) {
    return 0;
}

// Pre-condition: A character pointer to a comma delimited string
// Post-condition: The string is split into segments and stored into the buffer
void getFields(char * record,char * buffer[BUFFER_SIZE]) {
    int i = 0;
    int buffer_i = 0;
    int record_len = strlen(record);
    char tmp_buffer[RECORD_SIZE+1];
    int tmp_i = 0;

    for(;i < record_len;i++) {
        if(record[i] != ',') {
            tmp_buffer[tmp_i++] = record[i];
        } else {
            tmp_buffer[tmp_i] = 0;
            strcpy(buffer[buffer_i++],tmp_buffer);
            tmp_i = 0;
        }
    }
}

int main(int argc,char * argv[]) {
    //database = "test.txt";
    //readDB();
    char * buffer[BUFFER_SIZE];
    getFields("google,geek,pass123",buffer);

    int i = 0;

    for(;i<BUFFER_SIZE;i++) {
        printf(buffer[i]);
        printf("\n");
    }

    return 0;
}

From what I see, the offending line is in the getFields() function:

strcpy(buffer[buffer_i++],tmp_buffer);

I am trying to copy the contents of tmp_buffer into the indexes of buffer. My program just keeps crashing. I don't know why. Could someone please help me? Thanks.

Was it helpful?

Solution 2

You need to allocate memory for each of the three entries into buffer. As it stands now, you are copying strings into the void.

You need something like this:

for(i=0;i<BUFFER_SIZE;i++) {
    buffer[i] = malloc(MAX_STR_SIZE);
}

And then free them when you are done at the end:

for(i=0;i<BUFFER_SIZE;i++) {
    free(buffer[i]);
}

OTHER TIPS

You're not initializing the char *buffer to point to anything, e.g. buffer[0] = malloc(SIZE).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top