Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: How to passing multidimesional array of string to function? |
| Date | 2012-07-24 18:16 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <slrn3vfsk0tpkd.4b4.ike@iceland.freeshell.org> (permalink) |
| References | <500d3b6c$0$17953$4fafbaef@reader1.news.tin.it> <jujhhk$g4j$1@dont-email.me> <500ed69b$0$17946$4fafbaef@reader1.news.tin.it> |
On 2012-07-24, Alter <no@alt.invalid> 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 */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-23 13:53 +0200
Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-23 08:54 -0400
Re: How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-24 19:08 +0200
Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 18:28 +0100
Re: How to passing multidimesional array of string to function? "Alter" <no@alt.invalid> - 2012-07-24 21:40 +0200
Re: How to passing multidimesional array of string to function? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-24 13:16 -0700
Re: How to passing multidimesional array of string to function? Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-24 13:22 -0700
Re: How to passing multidimesional array of string to function? Fred K <fred.l.kleinschmidt@gmail.com> - 2012-07-24 13:22 -0700
Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 22:19 +0100
Re: How to passing multidimesional array of string to function? Stephen Sprunk <stephen@sprunk.org> - 2012-07-24 19:45 -0500
Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 22:07 +0100
Re: How to passing multidimesional array of string to function? Ike Naar <ike@iceland.freeshell.org> - 2012-07-24 18:16 +0000
Re: How to passing multidimesional array of string to function? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-23 14:07 +0100
Re: How to passing multidimesional array of string to function? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-07-23 08:05 -0700
Re: How to passing multidimesional array of string to function? pete <pfiland@mindspring.com> - 2012-07-23 22:51 -0400
Re: How to passing multidimesional array of string to function? Varun Tewari <tewari.varun@gmail.com> - 2012-07-24 10:21 -0700
Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-24 13:50 -0400
Re: How to passing multidimesional array of string to function? C3PO <nospam@nospam.com> - 2012-07-28 20:41 +0000
Re: How to passing multidimesional array of string to function? Keith Thompson <kst-u@mib.org> - 2012-07-28 15:04 -0700
Re: How to passing multidimesional array of string to function? Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-07-28 22:07 -0400
Re: How to passing multidimesional array of string to function? Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-07-29 11:33 +0300
Re: How to passing multidimesional array of string to function? aftnix <aftnix@gmail.com> - 2012-07-26 13:35 -0700
Re: How to passing multidimesional array of string to function? Barry Schwarz <schwarz45@yahoo.com> - 2012-07-27 15:05 -0700
csiph-web