Thread: Dealing With Multi Dim Char Arrays

  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Dealing With Multi Dim Char Arrays

    I'm trying to write a prog that reads a dictionary and writes the contents to a 2D character array. I'm having a bit of trouble though mainly with passing the array into a function. This compiles, but crashes. I think its because I need to allocate memory in the function or something.
    Code:
    #include <stdio.h>
    #define MAXLEN 30
    
    static FILE *OpenFile(char *file, char *mode);
    void LoadDictionary(char *dict[0][0]);
    
    int main()
    {
        char dictionary[MAXLEN][250000];
        LoadDictionary(&dictionary[0][0]);
        
        getchar();
        
    }    
    
    void LoadDictionary(char *dict[0][0])
    {
        char c;
        int x=0, y=0;
        FILE *in;    
        
        in=OpenFile("Dictionary,txt", "r");
        while((c=fgetc(in)) != EOF)
        {   
            if(c == '\n')
            {    
                c='\0';  
                dict[x][y]=c;  
                y++;      
            }
            else
                dict[x][y]=c;  
            x++; 
            if(x > MAXLEN-1)x=0;  
        } 
        fclose(in);        
    }
    
    static FILE *OpenFile(char *file, char *mode)
    {
        FILE *fp = fopen (file, mode);
        if (fp == NULL) 
        {
            perror ("Unable to open file");
            return NULL;
        }
        return fp;
    }
    Also. If, say I wanted to print the first hundred lines from my character array, how would I do that? This is an example of a guess I made, but I doubt it would work:
    Code:
        for(i=0; i<100; i++)
            printf("%s", dictionary[0][i]);

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    You're trying to allocate a 7.5 million byte static array, which is probably too big. Try allocating the memory dynamically instead. (And to test whether this is the problem, try reducing the size by say a factor of 10 or 100, and see what happens.)

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok, I'll do that. Wouldent have thought that 7.5MB should be a problem though. Big arrays like screen buffers dont use much less.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > char dictionary[MAXLEN][250000];
    Maybe it doesn't like 7.5MB of stuff on the stack, when many systems have a default limit of 1MB.

    Though quite why you only have 30 words with up to 250000 chars in their definition is strange.

    > void LoadDictionary(char *dict[0][0]);
    Given your array, the prototype should be
    void LoadDictionary( char dict[ ][250000] );

    And you call it with
    LoadDictionary( dictionary );
    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.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok thanks Salem, it compiles now.

    Though quite why you only have 30 words with up to 250000 chars in their definition is strange.
    I was imagining the contents of the array as if drawn out where words would be stored in rows.

    Anyway, now I am trying to print out the array entries. What I got is a bit of a mess:
    Code:
        int y, x;
        for(y=0; y<100; y++)
            for(x=0; x<30; x++)
                if(dictionary[x][y]=='\0')
                {
                   printf("\n"); 
                   break;
                }
                else
                    printf("%c", dictionary[x][y]);
    Is there any way to use "%s" in a printf to print out the nth row/column in a multi dim array?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Given
    char arr[X][Y];

    If each row has a \0, then
    for ( i = 0 ; i < X ; i++ ) printf("%s\n", arr[ i ] );
    prints a whole row in one step.
    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.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Ok, now this makes sense why my x/y are in the wrong order. Cheers.

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I have been trying to change my program to use a dynamic array, instead of a static one. I'm getting MAVs at the moment and I'm not sure if its possible to do this the way I am trying to do it.

    Basically my struct that holds a word is 20 Bytes. I allocated memory for 250000 of them (first line in bold). Then when I load up the dictionary I send the struct pointer in, and the struct size. When a word is loaded the pointer gets incremented by 20 to jump to the next word, but this wont seem to work.

    The only reason that I can think of for this not to work would be that the memory allocated might not be contiguous, but maybe its something else. Any ideas?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define MAXLEN 20
    #define MAXIT 250000
    
    struct dir
    {
        char word[MAXLEN];
    };  
    
    static FILE *OpenFile(char *file, char *mode);
    void LoadDictionary(dir *ptr, int size);
    
    int main()
    {
        struct dir *p;
       p = (struct dir *) malloc(MAXIT * MAXLEN); 
        LoadDictionary(p, MAXLEN);
        getchar();  
    }    
    
    void LoadDictionary(dir *ptr, int size)
    {
        char c;
        int x=0, y;
        FILE *in;    
        
        in=OpenFile("Dictionary.txt", "r");
        while((c=fgetc(in)) != EOF)
        {   
            if(c == '\n' || x==19)
            {    
                ptr->word[x]='\0';
                x=0;
                ptr+=size; //Jump to next entry. Wont work 
            }
            else
            {
                ptr->word[x]=c;  
                x++; 
            }
        } 
        fclose(in);        
    }
    
    static FILE *OpenFile(char *file, char *mode)
    {
        FILE *fp = fopen (file, mode);
        if (fp == NULL) 
        {
            perror ("Unable to open file");
            return NULL;
        }
        return fp;
    }

  9. #9
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I worked out what was wrong. Just had to increment the pointer by 1 instead of the struct size. This always seems to happen to me just after I post.
    Last edited by mike_g; 06-15-2007 at 01:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM