multidimentional array of strings ???

This is a discussion on multidimentional array of strings ??? within the C Programming forums, part of the General Programming Boards category; hello. can anyone help in declaring and allocating mem for a dynamic 2-dimensional array of strings? what i try to ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    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

    null
    any ideas?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    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.
    Code:
    #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 );
            num_products++;
    
            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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    32

    Thumbs up

    Hi Salem. You'r right, that seems a much better way to do it...so 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, 01:59 PM
  2. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  3. Build an array of strings dynamically
    By Nazgulled in forum C Programming
    Replies: 29
    Last Post: 04-07-2007, 10: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, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21