Re: How to passing multidimesional array of string to function?

Discussion in 'C Programming' started by Eric Sosman, Jul 23, 2012.

  1. Eric Sosman

    Eric Sosman Guest

    On 7/23/2012 7:53 AM, Alter wrote:
    > Hi all
    >
    >
    > How to pass a multidimensional array of string to a function?


    This might be Question 6.19 on the comp.lang.c Frequently
    Asked Questions (FAQ) page at <http://www.c-faq.com/>. Or it
    might be 6.18, or 6.20, or -- Tell you what: Read Section 6 of
    the FAQ, and post again with more detail if things still aren't
    clear.


    --
    Eric Sosman
    d
     
    Eric Sosman, Jul 23, 2012
    #1
    1. Advertising

  2. "Alter" <> writes:

    > This type of array:
    >
    > char *word[50][50]={{"123", "3048548t", "fdòfkd9ds8","0"," ",...},...}
    >
    > As it passed to the function?
    >
    >
    > Must be written as the prototype of the function, and should be used?


    You can write

    void f(char *wa[][50]);

    or you can use the slightly cryptic

    void f(char *(*wa)[50]);

    These are not the only ways, but they are probably the most idiomatic.

    --
    Ben.
     
    Ben Bacarisse, Jul 24, 2012
    #2
    1. Advertising

  3. Eric Sosman

    Ike Naar Guest

    On 2012-07-24, Alter <> wrote:
    > This type of array:
    >
    > char *word[50][50]={{"123", "3048548t", "fd?fkd9ds8","0"," ",...},...}
    >
    > As it passed to the function?
    >
    > Must be written as the prototype of the function, and should be used?


    Please read chapter 6 of the C FAQ if you haven't already done so.

    Now, here's one possibility:

    /* begin example0.c */

    #include <string.h>
    #include <assert.h>

    static void check(char *word[][50], int row, int col, char const *expect)
    {
    assert(0 == strcmp(word[row][col], expect));
    }

    int main(void)
    {
    char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
    check(word, 1, 2, "1:2");
    check(word, 2, 1, "2:1");
    return 0;
    }

    /* end example0.c */


    Here's another possibilty, using variable length arrays
    (works with C99 or newer).
    It has the advantage that you can use the function for any
    two-dimensional array of strings, regardless the size.

    /* begin example1.c */

    #include <string.h>
    #include <assert.h>

    static void check(int nrows, int ncols, char *word[nrows][ncols],
    int row, int col, char const *expect)
    {
    assert(0 == strcmp(word[row][col], expect));
    }

    int main(void)
    {
    char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
    check(50, 50, word, 1, 2, "1:2");
    check(50, 50, word, 2, 1, "2:1");
    return 0;
    }

    /* end example1.c */


    Here's another one, that adds a level of indirection, so that
    a pointer to the array is passed, instead of passing the array.
    The advantage is that the compiler will verify that both dimensions
    of the passed array match exactly.

    /* begin example2.c */

    #include <string.h>
    #include <assert.h>

    static void check(char *(*word)[50][50], int row, int col, char const *expect)
    {
    assert(0 == strcmp((*word)[row][col], expect));
    }

    int main(void)
    {
    char *word[50][50] = {{"0:0"}, {"1:0", "1:1", "1:2"}, {"2:0", "2:1"}};
    check(&word, 1, 2, "1:2");
    check(&word, 2, 1, "2:1");
    return 0;
    }

    /* end example2.c */
     
    Ike Naar, Jul 24, 2012
    #3
  4. בת×ריך ×™×•× ×©×œ×™×©×™, 24 ביולי 2012 20:40:22 UTC+1, מ×ת Alter:
    > Hi
    >
    >
    > I mean as an array multidimesional, a two-dimensional array.
    >
    >

    C doesn't have good syntax for two dimensional arrays. Its' easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
    Its' best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.
     
    Malcolm McLean, Jul 24, 2012
    #4
  5. Eric Sosman

    Fred K Guest

    On Tuesday, July 24, 2012 1:16:42 PM UTC-7, Malcolm McLean wrote:
    > בת×ריך ×™×•× ×©×œ×™×©×™, 24 ביולי 2012 20:40:22 UTC+1, מ×ת Alter:
    > &gt; Hi
    > &gt;
    > &gt;
    > &gt; I mean as an array multidimesional, a two-dimensional array.
    > &gt;
    > &gt;
    > C doesn't have good syntax for two dimensional arrays. Its' easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
    > Its' best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.


    Unless the 2D array was created by creating a 1D array of pointers to 1d arrays.
     
    Fred K, Jul 24, 2012
    #5
  6. Eric Sosman

    Fred K Guest

    On Tuesday, July 24, 2012 1:22:03 PM UTC-7, Fred K wrote:
    > On Tuesday, July 24, 2012 1:16:42 PM UTC-7, Malcolm McLean wrote:
    > &gt; בת×ריך ×™×•× ×©×œ×™×©×™, 24 ביולי 2012 20:40:22 UTC+1, מ×ת Alter:
    > &gt; &amp;gt; Hi
    > &gt; &amp;gt;
    > &gt; &amp;gt;
    > &gt; &amp;gt; I mean as an array multidimesional, a two-dimensional array..
    > &gt; &amp;gt;
    > &gt; &amp;gt;
    > &gt; C doesn&amp;#39;t have good syntax for two dimensional arrays. Its&amp;#39; easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
    > &gt; Its&amp;#39; best to treat your 2d arrays as 1d arrays, and do the calculation manually. array[y*width+x] will give you the element at y, x.
    >
    > Unless the 2D array was created by creating a 1D array of pointers to 1d arrays.


    (In which case it isn't really a 2D array)
     
    Fred K, Jul 24, 2012
    #6
  7. "Alter" <> writes:

    > I mean as an array multidimesional, a two-dimensional array.


    Are the answers you've had unsuitable? If so, just repeating yourself
    like this probably won't get you closer to whatever the real problem is.

    --
    Ben.
     
    Ben Bacarisse, Jul 24, 2012
    #7
  8. Malcolm McLean <> writes:

    > בת×ריך ×™×•× ×©×œ×™×©×™, 24 ביולי 2012 20:40:22 UTC+1, מ×ת Alter:
    >>
    >> I mean as an array multidimesional, a two-dimensional array.
    >>

    > C doesn't have good syntax for two dimensional arrays.


    That's not good advice to give beginners because it's such a loaded
    value judgement.

    To the OP: C has syntax for two dimensional arrays and you should make
    your own mind up if it is "good" after you learn about it. Many people
    find it perfectly adequate for many tasks. No language has perfect
    arrays, ideal for all circumstances[1].

    > Its' easy enough to declare them, but to resize them , pass them
    > about, and extract .lines and columns is difficult. Its' best to
    > treat your 2d arrays as 1d arrays, and do the calculation
    > manually. array[y*width+x] will give you the element at y, x.


    That's bad advice. Doing this does not simplify resizing or accessing
    columns. Accessing rows is perfectly simple using both methods. What
    the problem is in "passing them about" I don't know.

    [1] Except Algol 68, of course. :)

    --
    Ben.
     
    Ben Bacarisse, Jul 24, 2012
    #8
  9. On 24-Jul-12 15:16, Malcolm McLean wrote:
    > בת×ריך ×™×•× ×©×œ×™×©×™, 24 ביולי 2012 20:40:22 UTC+1, מ×ת Alter:
    >> I mean as an array multidimesional, a two-dimensional array.

    >
    > C doesn't have good syntax for two dimensional arrays.


    C does have a syntax for multidimensional arrays; how "good" it may be
    is purely in the eye of the beholder. IMHO, it does an adequate job of
    doing what it's designed to do.

    > Its' best to treat your 2d arrays as 1d arrays, and do the
    > calculation manually. array[y*width+x] will give you the element
    > at y, x.


    That's all the C array subscript operator does internally anyway, so you
    have thrown away the syntactic sugar and type checking for no real
    advantage--and increased the opportunity for bugs.

    Do you propose everyone use (*ptr).field instead of ptr->field as well?

    S

    --
    Stephen Sprunk "God does not play dice." --Albert Einstein
    CCIE #3723 "God is an inveterate gambler, and He throws the
    K5SSS dice at every possible opportunity." --Stephen Hawking
     
    Stephen Sprunk, Jul 25, 2012
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Bacarisse
    Replies:
    0
    Views:
    379
    Ben Bacarisse
    Jul 23, 2012
  2. Malcolm McLean
    Replies:
    0
    Views:
    380
    Malcolm McLean
    Jul 23, 2012
  3. Varun Tewari
    Replies:
    5
    Views:
    420
    Phil Carmody
    Jul 29, 2012
  4. aftnix
    Replies:
    0
    Views:
    358
    aftnix
    Jul 26, 2012
  5. Barry Schwarz
    Replies:
    0
    Views:
    359
    Barry Schwarz
    Jul 27, 2012
Loading...

Share This Page