Thread: passing 2d char array to a function

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    44

    passing 2d char array to a function

    I know how to pass in 2d char arrays if i'm dealing with the first dimension of the array:
    Code:
    void fromAbove(char array[],int row)
    {
            char temp;
    
            temp = array[row];
            array[row] = array[row - 1];
            array[row - 1] = temp;
    }
    and that to return that in the main function, I use the array I want as input, and put the second dimension in:
    Code:
     fromAbove(puzzle[column],row);
    but I have absolutely no idea how to do that when I refer only to the second dimension. Other posts I read say that I need to define the length of the second dimension, but I don't know if I can do that since I think I'd need a for loop inside the function and all I need to do is run the function when a certain value is found within the original 2d array.

    If it helps, the program is one of those 5x5 puzzles (read using file i/o, printed to another file after functions are run on it) with a space in it where you slide around pieces until you get a picture. Except this is with letters, and instead of solving it, I'm just writing a program that runs a number of moves on it.

    EX:

    TRGSJ
    XDOKI
    M VLN
    WPABE
    UQCHF
    ARRBBL

    where line 6 is the line of instructions for what moves to make. I have the functions written for moves that involve moving the space up or down, but can't figure out moving right and left. I know I need to do a [column + 1] and a [column - 1], but have no idea how to pass the array into a function.

    Thanks for your time =]

  2. #2
    Registered User
    Join Date
    Feb 2011
    Posts
    3
    What you actually want to do is pass a pointer to the memory holding your 2D array. In C, we typically don't want to pass large, contiguous regions of data by value since it is inefficient, so we pass their locations (addresses) instead.

    Code:
    void fromAbove(char* array[], int rows)
    char* array[] is a pointer to an array or arrays of characters. Since each array of characters
    is, by C convention, terminated by a null character, we only have to tell the function how many rows there are so long as we're mindful about not reading or writing past these nulls.

    To access an element in row R and column C of this 2D array, use "array[R][C]".

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    I understand why pointers would be useful, and I experimented with them earlier, but unless I was using them completely incorrectly, I didn't find it that useful in creating the output I wanted. I figured that might have been due to the fact that instead of returning an entire array from the function, I'm only applying the function to one specific instance:
    Code:
    if(puzzle[row][column] == ' ')
    {
         fromAbove(puzzle[column],row);
    }
    Quote Originally Posted by kittykitty View Post
    To access an element in row R and column C of this 2D array, use "array[R][C]".
    For this part of your post, I don't quite understand what you're saying...would this be in the function or where I call it? If I use it in the function definition I'll get a compiler error stating that R and C are undeclared variables. If you're referring to just referencing the value in the array specifically, that part I have covered for the most part; I just need to know how to reference the second dimension [column] in a function definition and how to call it, since the first dimension [row] is a constant for the stated situations.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Code:
    if(puzzle[row][column] == ' ')
    {
         fromAbove(puzzle[column],row);
    }
    Decide whether the first dimension is row or column. Assuming columns are the first dimension, you cannot generate a pointer pointing to a row. You should pass pointer to all the elements.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    44
    Quote Originally Posted by kmdv View Post
    Code:
    if(puzzle[row][column] == ' ')
    {
         fromAbove(puzzle[column],row);
    }
    Decide whether the first dimension is row or column. Assuming columns are the first dimension, you cannot generate a pointer pointing to a row. You should pass pointer to all the elements.
    the call for the function is standard when in the function you deal with the first dimension. if you look at the OP, when i write the code for the function, it reads as
    Code:
    void fromAbove(char array[],row)
    in that sense, i've already decided row as first dimension and column as second.

    in regards to pointers, another method i tried was
    Code:
    void fromAbove(char array[5][5]) //don't remember if i defined any variables after that
    
    //back in the main function, the call looks like:
    fromAbove(**puzzle);
    however, that assumes i want to change both dimensions, which i do not.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read: Arrays and Pointers

    Pay attention to point 6.18.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing 2d array to function
    By Tupcia in forum C Programming
    Replies: 3
    Last Post: 04-13-2008, 11:33 AM
  2. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  3. Passing char array into function
    By Crysist in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2005, 12:03 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM