Thread: 2D arrays and storing characters

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    2D arrays and storing characters

    I want to read in a file of characters and store each character inside a 2D array. The array will be 8x8 and the file will always contain the same dimensions of characters. I want to treat the array as a 1D array, access the memory allocated for it at array[0][0] and start filling it all the way up to
    array[7][7]. I don't want to use two for loops, with subscripting like [0][0], just look it as an array from 0-63, array[63] would be the last element....but only in memory, in reality it would still be a 2D array. How can I access the beginning of the array all the way to the end element by element? Will using array[0], array[1] etc.. just give me the entire row? Any help would be appreciated.

    Code:
    char actual_array[8][8];
    int arr* = &actual_array[0][0]; //pointer to the beginning or the array
    int j = 0;
    
    while( (ch = fgetc( theFile )) != feof( input ) )
    {
        if( j < (8*8) )
        {
            arr[ j++ ] = ch; //is this right? can I advance the pointer like this?
        }
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yes I'm pretty sure that would be possible, but first you have a few problems. First off you have an integer pointer to a char, you should be using a char pointer for it. Also your asterisk for the pointer is on the wrong side of the variable name. This should look like:
    Code:
        char actual_array[8][8];
        char *arr = &actual_array[0][0];

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    thanks, what about passing arrays as parameters to a function. I want to use a char *array to hold full length strings of file names.

    Code:
    main()
    {
    char *array[4] //4 files
    
    array[0] = "somefile.txt";
    ...
    ...
    ...
    
    myfunction( array[0] ); // pass the file name
    
    }
    
    myfunction( char *file ) //is this acceptable?
    {
    
    fopen( file, "w" );  //will it now open somefile.txt?
    
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    it should be const char *
    you also missing return types of main and myfunction
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    I just jotted the functions down quickly as an example, I also don't think return statments are necessary unless following proper practices, but that's a debate for another time. I don't really care about protecting the date by using the const keyword. I simply want to pass a string as a parameter for reading the file in myfunction. Since I technically store a string in the array in main(), I want to pass that "string" to myfunction(). Is my code adequate as it is, i.e. will it do the job i just described? Or is my method of reasoning flawed?

    Because from what I see of the code, when I call fopen, it has a pointer as an argument which points to "somefile.txt" in memory, so that command will execute and do what it is supposed to do....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing strings in arrays
    By smp in forum C Programming
    Replies: 6
    Last Post: 12-16-2008, 09:37 AM
  2. reading file and storing to arrays
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 04-27-2006, 05:17 AM
  3. Storing integers from file into arrays???? please help
    By adzza69 in forum C++ Programming
    Replies: 5
    Last Post: 09-11-2004, 12:28 AM
  4. Storing strings in 2d char arrays problem
    By rainmanddw in forum C++ Programming
    Replies: 5
    Last Post: 10-22-2003, 05:41 PM
  5. integers storing as symbols in arrays
    By rjcarmo in forum C++ Programming
    Replies: 4
    Last Post: 05-19-2003, 01:17 AM