Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 23 Jul 2012 21:51:17 -0500 Message-ID: <500E0DA2.3075@mindspring.com> Date: Mon, 23 Jul 2012 22:51:14 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: How to passing multidimesional array of string to function? References: <500d3b6c$0$17953$4fafbaef@reader1.news.tin.it> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 152 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.78 X-Trace: sv3-UTbn9k3u236WGiKMKac7fOqWMG4UCLtXcpaj6PRCDekoUyw9Zs/2JFun49sFzr2AKPp1AUHe57onD3n!t7xU6ZBgDLdjB9MYPMGBk5j8r8G/nCeLPo1KkcWAgWuj76HXh1oHx5mkz5PTJxmFMZRBPSEu1Gk4!xaI9slir7g== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 5328 Xref: csiph.com comp.lang.c:23642 Alter wrote: > > Hi all > > How to pass a multidimensional array of string to a function? > > Thanks in advance > > Cheers /* BEGIN new.c */ #include #include #include #define SORT_FUNCTIONS \ no_sort, "Original order of the test arrays:", \ slsort, "Nonstable simple selection sort:", \ i_sort, "Stable insertionsort:", \ qsort, "Standard library qsort;\n" \ "unspecified ordering of equal keys:" #define NUMBERS \ "zero","one","two","three","four","five", \ "six","seven","eight","nine","ten" #define NMEMB(A) (sizeof (A) / sizeof *(A)) int comp(const void *a, const void *b); void no_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); void slsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); void i_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); int main(void) { size_t element, sort; struct sf { void (*func)(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)); const char *description; } s_F[] = {SORT_FUNCTIONS}; const char numbers[][sizeof "three"] = {NUMBERS}; char array[NMEMB(numbers)][sizeof "three"]; puts("\n/* BEGIN output from new.c */\n\n" "Arrays of (char [sizeof \"three\"]),\n" "are being sorted by string length.\n"); for (sort = 0; sort != NMEMB(s_F); ++sort) { memcpy(array, numbers, sizeof array); s_F[sort].func(array, NMEMB(array), sizeof*array, comp); puts(s_F[sort].description); for (element = 0; element != NMEMB(array); ++element) { puts(array[element]); } putchar('\n'); } puts("/* END output from new.c */"); return 0; } int comp(const void *a, const void *b) { const char (*const aaa)[sizeof "three"] = a; const char (*const bbb)[sizeof "three"] = b; const char *aa = *aaa; const char *bb = *bbb; while (*aa != '\0' && *bb != '\0') { ++aa; ++bb; } return *bb != '\0' ? -1 : *aa != '\0'; } void no_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { base, nmemb, size, compar; } void slsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { size_t tail; unsigned char *array, *first, *middle, *end, swap; if (nmemb-- > 1) { end = base; do { array = end; end += size; first = middle = end; tail = nmemb; while (--tail != 0) { middle += size; if (compar(first, middle) > 0) { first = middle; } } if (compar(array, first) > 0) { do { swap = *first; *first++ = *array; *array++ = swap; } while (array != end); } } while (--nmemb != 0); } } void i_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) { unsigned char *array, *low, *high, *p1, *p2, swap; if (nmemb-- > 1) { array = base; do { low = array; high = low + size; array = high; while (compar(low, high) > 0) { p1 = high; p2 = low; do { swap = *p1; *p1++ = *p2; *p2++ = swap; } while (p2 != high); if (low == base) { break; } high = low; low -= size; } } while (--nmemb != 0); } } /* END new.c */ -- pete