Thread: Two dimensional char array

  1. #16
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    Alright, I understand that

    now does that apply for needing 2 elements per line

    am I not allocating enough to have 2 elements(each with 30 chars) on x number lines?


    should I
    Code:
    char ***c2names;
    
    c2names = (char ***) malloc ( amt_of_lines * sizeof(char **);
    for(i = 0; i < amt_of_lines; i++)
    {
    c2names[i] = (char **) malloc(columns * sizeof(char *);
    c2names[i][0] = (char *) malloc(30 * sizeof(char);
    c2names[i][1] = (char *) malloc(30 * sizeof(char);
    }
    Last edited by eurus; 01-18-2006 at 11:47 PM.

  2. #17
    Registered User
    Join Date
    Jan 2006
    Posts
    23
    No compile errors or anything but wondering if thats bad style/practice.

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    am I not allocating enough to have 2 elements(each with 30 chars) on x number lines?
    No, you are not. You are allocating memory for amt_of_lines * 30 characters. If each word needs 30 characters (and you have 2 words), then you need to allocate twice as much memory.

    Your new code looks good except for the fact you are missing some closing parethesis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  2. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. Passing structures... I can't get it right.
    By j0hnb in forum C Programming
    Replies: 6
    Last Post: 01-26-2003, 11:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM