Dear all,

In the function below,

how should the array of arrays data structure be defined and used?

void foo(int option)
{
    int i =0;
    char Colstr[3][15] = {"Red","Blue","Green"};
    char Volstr[7][15] = {"1","2","3","3.5","4","4.5","5"};
    char Sndstr[4][15] =  {"lo","med","hi","vhi"};
    int numSTrings[2] = {3,7,4};

    char *arrayOfarrays[] = {Colstr,Volstr,Sndstr}; // How should this be declared?

      for(i=0;i<numSTrings[option];i++)
            printf("%s\n", ????);                   // How should arrayOfarrays be referred?
}

thanks,
shri

Recommended Answers

Directly you'd do it like this:

#include <stdio.h>

#define length(a) (sizeof (a) / sizeof *(a))

int main(void)
{
    char Colstr[3][15] = {"Red", "Blue", "Green"};
    char Volstr[7][15] = {"1", "2", "3", "3.5", "4", "4.5", "5"};
    char Sndstr[4][15] = {"lo", "med", "hi", "vhi"};
    int n[3] = {3, 7, 4}; …
Jump to Post

All 3 Replies

Directly you'd do it like this:

#include <stdio.h>

#define length(a) (sizeof (a) / sizeof *(a))

int main(void)
{
    char Colstr[3][15] = {"Red", "Blue", "Green"};
    char Volstr[7][15] = {"1", "2", "3", "3.5", "4", "4.5", "5"};
    char Sndstr[4][15] = {"lo", "med", "hi", "vhi"};
    int n[3] = {3, 7, 4};
    int i, j;

    char (*a[])[15] = {Colstr, Volstr, Sndstr};

    for (i = 0; i < length(a); i++) {
        for (j = 0; j < n[i]; j++) {
            printf("'%s'\t", a[i][j]);
        }

        putchar('\n');
    }

    return 0;
}

But that's awkward. I'd recommend using a typedef for your string instead:

#include <stdio.h>

#define length(a) (sizeof (a) / sizeof *(a))

typedef char char15[15];

int main(void)
{
    char15 Colstr[3] = {"Red", "Blue", "Green"};
    char15 Volstr[7] = {"1", "2", "3", "3.5", "4", "4.5", "5"};
    char15 Sndstr[4] = {"lo", "med", "hi", "vhi"};
    int n[3] = {3, 7, 4};
    int i, j;

    char15 *a[] = {Colstr, Volstr, Sndstr};

    for (i = 0; i < length(a); i++) {
        for (j = 0; j < n[i]; j++) {
            printf("'%s'\t", a[i][j]);
        }

        putchar('\n');
    }

    return 0;
}

This greatly simplifies the syntax and also more clearly shows your intentions. It's also easier to understand for folks who aren't intimately familiar with the dark corners of C's declaration syntax.

One way is like this

void foo(int option)
{
    int i =0;
    char *Colstr[] = {"Red","Blue","Green"};
    char *Volstr[] = {"1","2","3","3.5","4","4.5","5"};
    char *Sndstr[] =  {"lo","med","hi","vhi"};
    int numSTrings[] = {3,7,4};

    char **arrayOfarrays[] = {Colstr,Volstr,Sndstr}; // How should this be declared?

      for(i=0;i<numSTrings[option];i++)
            printf("%s\n", ????);                   // How should arrayOfarrays be referred?
}

Thanks deceptikon for the valuable inputs!

cheers,
Shri

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.