Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #23642

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

Message-ID <500E0DA2.3075@mindspring.com> (permalink)
Date 2012-07-23 22:51 -0400
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: How to passing multidimesional array of string to function?
References <500d3b6c$0$17953$4fafbaef@reader1.news.tin.it>

Show all headers | View raw


Alter wrote:
> 
> Hi all
> 
> How to pass a multidimensional array of string to a function?
> 
> Thanks in advance
> 
> Cheers

/* BEGIN new.c */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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