Velocity Reviews > Re: How to passing multidimesional array of string to function?

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

Eric Sosman
Guest
Posts: n/a

 07-23-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d

Ben Bacarisse
Guest
Posts: n/a

 07-24-2012
"Alter" <(E-Mail Removed)> 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.

Ike Naar
Guest
Posts: n/a

 07-24-2012
On 2012-07-24, Alter <(E-Mail Removed)> 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?

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
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 */

Malcolm McLean
Guest
Posts: n/a

 07-24-2012
בתאריך יום שלישי, 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.

Fred K
Guest
Posts: n/a

 07-24-2012
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&#39;t have good syntax for two dimensional arrays. Its&#39; easy enough to declare them, but to resize them , pass them about, and extract .lines and columns is difficult.
> Its&#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.

Fred K
Guest
Posts: n/a

 07-24-2012
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)

Ben Bacarisse
Guest
Posts: n/a

 07-24-2012
"Alter" <(E-Mail Removed)> writes:

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

like this probably won't get you closer to whatever the real problem is.

--
Ben.

Ben Bacarisse
Guest
Posts: n/a

 07-24-2012
Malcolm McLean <(E-Mail Removed)> 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
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.

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.

Stephen Sprunk
Guest
Posts: n/a

 07-25-2012
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