-2

For my cs project I need to use a 2d table of strings, which will symulate database table. I don't know how to create such thing. Table should have 5 columns and number of rows should be increasing when new records are added. The max size of strings inside is 52 signs.

  • 1
    Lots of questions about this, first I found is [this](http://stackoverflow.com/questions/32834711/how-to-initialize-rows-of-2d-array-of-strings-in-c) and there are plenty more. – Weather Vane Dec 20 '15 at 19:30
  • Reading the question more carefully, you actually need a **3D** array? Two dims for a table, a third for cell content, because C does not have strings, only `char` arrays. – Weather Vane Dec 20 '15 at 19:36
  • take a look at this http://stackoverflow.com/questions/1768294/how-to-allocate-a-2d-array-of-pointers-in-c – Mustapha Dec 20 '15 at 19:44
  • @MustaphaBenDaou that is C++. C does not have `new`. – Weather Vane Dec 20 '15 at 19:55

2 Answers2

0

You have to create a dynamic 3d array to hold row, column and the char array.

To do this initiate the 3d-array with pointers

char ***table;

Each time you then add a new row to your "database" you have to expand the memory allocated to your table. I would suggest to initiate another variable which holds the current size of your table. To expand the memory you need by adding a new row you have to assign the amount of memory for current rows * (size-pointer) + size pointer. Then you have to allocate memory to this new pointer (size-pointer) * 5 for your columns. Then for each column assign the size of memory needed to the pointer of each column by either just allocating the max of 52 * (size-char) or even only the size of the string you want to save at the respective position. Use malloc() for adding a new value to your table and realloc() to change the size of existing levels, f.e. when adding a new row, because you then change the amount of memory allocated to your row-level.

This answer assumes you're familiar with dynamic arrays and memory allocation, as well as accessing them. Please leave a comment if you need further explanation or code examples as the above only discusses an idea how to deal with the problem.

Some examples:

//adding a new row
table = realloc(table, sizeof(char*)*(current_size+1));
table[current_size+1] = malloc(sizeof(char*)*5);
//now for every column do the following where i is the number for the current column
table[current_size+1][i] = malloc(52*sizeof(char));

You can then access the new created fields like so (i and k are for respective row and column):

table[i][k] = "Hello World";

Hope it helps!

hallole
  • 179
  • 8
  • I will try to do that, but a bit of realloc and malloc code would be helpfull. Also thanks for help. – user3426015 Dec 20 '15 at 21:10
  • I added an example on how to adjust and manage memory for a new row; the code is untested though, but in principle it should work. – hallole Dec 20 '15 at 21:24
  • int i=0; database = realloc(database, sizeof(char*)*(current_size+1)); database[current_size+1] = malloc(sizeof(char*)*5); while(i<5) { database[current_size+1][i] = malloc(52*sizeof(char)); ++i; } is it ok? – user3426015 Dec 20 '15 at 22:20
  • Should work I assume – hallole Dec 20 '15 at 22:28
0

You're probably going to need to define a data structure to use with your db, in case you need to search on the data. Something as simple as a list may suffice, depending on the complexity of your assignment. Here is a VERY simple example:

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

typedef struct db
{
    char col1[52];
    char col2[52];
    char col3[52];
    char col4[52];
    char col5[52];
    struct db *next;
} DB_type;

DB_type* add_reg(DB_type* db_ptr);

int main(void)
{
    DB_type *db_ptr = NULL;

    db_ptr = add_reg(db_ptr);
    db_ptr = add_reg(db_ptr);
    db_ptr = add_reg(db_ptr);

    DB_type *aux = db_ptr;
    while (aux)
    {
        printf("%s %s %s %s %s\n", aux->col1, aux->col2, aux->col3, aux->col4,
                aux->col5);
        aux = aux->next;
    }

    return 0;
}

DB_type* add_reg(DB_type* db_ptr)
{
    DB_type *new_data = (DB_type*) malloc(sizeof(DB_type));
    printf("Add new data:\n");
    scanf("%s %s %s %s %s", new_data->col1, new_data->col2, new_data->col3,
            new_data->col4, new_data->col5);
    printf("Done.\n");
    if (db_ptr)
        new_data->next = db_ptr;
    else
        new_data->next = NULL;
    return new_data;
}
Bruno S
  • 29
  • 3