Is 2D array enough?

This is a discussion on Is 2D array enough? within the C Programming forums, part of the General Programming Boards category; In C# or C++ it would be enough to use 2D array, but how about in C? The data I ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    19

    Is 2D array enough?

    In C# or C++ it would be enough to use 2D array, but how about in C? The data I want to have in array is a directory listing like this

    Code:
    File name                   Date               Size     Rank
    c:\folder\other.jpg         20100107-160340      400    2
    c:\directory\something.doc  20100107-230744    15000    1
    (which of first row is not actually part of the array, just a header line)

    The array has nothing but text in it (numbers are treated as text too), but does that mean I need first dimension for column (file name, rank, etc.), second for each file (other.jpg, something.doc) and a third one for each string I have there, so in this case should the array be just
    char data[2][4], or does it have to be
    char data[2][4][13]
    ?

    What's the smartest way to stuff this kind of info into an array?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Why not just create a new struct to hold that information:

    Code:
    struct data{
      char  *file_name;
      char *date;
     char *size;
     char *rank;
    };
    
    struct data my_data[100];
    Or, if you are not comfortable with pointers you can just allocate a fixed size for char arrays.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    19
    Yeah, I think using struct might prove to be easier.

    When using C "not comfortable with pointers" is a pretty darn big flaw in my opinion ie. anyone using C should learn to use pointers in my opinion (that's what I'm trying to do right now).

    Thanks for the answer!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocate space for 2d array
    By jtay in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 11:34 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 11:33 AM
  3. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 04:45 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 01:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM

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