Thread: multidimentional array of strings ???

  1. #1
    Registered User
    Join Date
    Sep 2001

    Question multidimentional array of strings ???

    hello. can anyone help in declaring and allocating mem for a dynamic 2-dimensional array of strings?

    what i try to do is the following: i'm reading a file that comntains records etc and want to make an array such as:

    product code |produc name |
    __(char *)______|___(char *)_____|

    the first column must contain the product code and the second must have the corresponding product name.

    both product codes and product names are strings (char *) and should be allocated dynamically. so it must do the whole array.

    i got a bit of a problem visualising arrays that got more than 3 dimensions (like in this case). with three dimensions i think of it as axes x,y,z. but with more i cant see how it works.

    any ideas?

    thanx a lot

    any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well the way to keep those two things associated is to use a structure, not a 2D array where the minor dimension is a small constant (ie 2).

    As well as being more readable, its also far more extendable.

    Here's the general idea of the bits you need - if you've got the file reading bit all figured out, shouldn't be too difficult.
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct {
        char *code;
        char *name;
    } product_st;
    // you may have strdup in your library
    char *string_dup ( char *name ) {
        char *res = malloc( strlen(name) + 1 );
        strcpy( res, name );
        return res;
    int main ( ) {
        char in_code[100], in_name[100];    // input strings
        int num_products = 0;               // number so far
        int max_products = 100;             // initial guess
        product_st *products = malloc( max_products * sizeof(product_st) );
        // read the file to get string
        // this goes inside your loop.
            products[num_products].code = string_dup( in_code );
            products[num_products].name = string_dup( in_name );
            if ( num_products == max_products ) {
                // read about realloc
        // end loop
        return 0;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2001

    Thumbs up

    Hi Salem. You'r right, that seems a much better way to do i did it your way.

    Thanx for the help...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. intializing an array of strings
    By doubty in forum C Programming
    Replies: 4
    Last Post: 06-19-2009, 12:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 09:35 PM
  4. Array of strings in C
    By szill in forum C Programming
    Replies: 10
    Last Post: 02-22-2005, 05:03 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM