dd@conus.info wrote:
Hello.
My primary goal is to initialize array of pointers to structures like
this:
struct R aa[]={
{"asd",{"dsa","dda"},{"441","882"}},
{"ddd",{"aaa","666"},{"111","772"}},
{"bbb",{"ddd","qqq"},{"551","222"}}};
Structure is: pointer to null-terminated string, and two pointers to
arrays of pointers to strings.
I'm try to declare structure as:
struct R
{
char *one;
char **two;
char **three;
};
But with no success. Does anybody knows how to do this?
Does this work with your compiler?
#include <stdio.h>
struct R
{
char *one;
char **two;
char **three;
};
int main(void)
{
struct R aa[] = {
{.one = "asd",
.two = (char *[]) {"dsa", "dda"},
.three = (char *[]) {"441", "882"}},
{.one = "ddd",
.two = (char *[]) {"aaa", "666"},
.three = (char *[]) {"111", "772"}},
{.one = "bbb",
.two = (char *[]) {"ddd", "qqq"},
.three = (char *[]) {"551", "222"}}
};
unsigned i, j;
for (i = 0; i < 3; i++) {
printf("aa[%u].one = %p, points to \"%s\"\n", i,
(void *) aa[i].one, aa[i].one);
printf("aa[%u].two = %p\n", i, (void *) aa[i].two);
printf("aa[%u].three = %p\n", i, (void *) aa[i].three);
for (j = 0; j < 2; j++) {
printf("aa[%u].two[%u] = %p, points to \"%s\"\n", i, j,
(void *) aa[i].two[j], aa[i].two[j]);
printf("aa[%u].three[%u] = %p, points to \"%s\"\n", i, j,
(void *) aa[i].three[j], aa[i].three[j]);
}
}
return 0;
}
[output]
aa[0].one = 1730, points to "asd"
aa[0].two = eff90
aa[0].three = eff88
aa[0].two[0] = 1700, points to "dsa"
aa[0].three[0] = 1708, points to "441"
aa[0].two[1] = 1704, points to "dda"
aa[0].three[1] = 1714, points to "882"
aa[1].one = 171c, points to "ddd"
aa[1].two = eff80
aa[1].three = eff78
aa[1].two[0] = 170c, points to "aaa"
aa[1].three[0] = 1710, points to "111"
aa[1].two[1] = 1718, points to "666"
aa[1].three[1] = 1720, points to "772"
aa[2].one = 1734, points to "bbb"
aa[2].two = eff70
aa[2].three = eff68
aa[2].two[0] = 171c, points to "ddd"
aa[2].three[0] = 1728, points to "551"
aa[2].two[1] = 1724, points to "qqq"
aa[2].three[1] = 172c, points to "222"