Two dimensional char array

This is a discussion on Two dimensional char array within the C Programming forums, part of the General Programming Boards category; Alright, I understand that now does that apply for needing 2 elements per line am I not allocating enough to ...

  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 10: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,246
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-17-2008, 11:36 AM
  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, 10:55 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

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