strange structure initialization

D

dd

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?
 
E

Emmanuel Delahaye

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;
};

You have an array of 2 pointers to char, hence you want:

struct R
{
char *one;
char *two[2];
char *three[2];
};

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
E

Emmanuel Delahaye

No-no. There're will be unknown number of pointers, not 2, maybe even
zero.

So, you need the first example you supplied, but the initialisation
must be done in separated steps :

int main (void)
{
struct R
{
char *one;
char **two;
char **three;
};

char *a[] =
{"dsa", "dda"};
char *b[] =
{"441", "882"};
char *c[] =
{"aaa", "666"};
char *d[] =
{"111", "772", "1234"};
char *e[] =
{"ddd", "qqq", "xyz", "abcd", "xxx"};
char *f[] =
{"551", "222"};


struct R aa[] =
{
{"asd", a, b},
{"ddd", c, d},
{"bbb", e, f}
};

return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"C is a sharp tool"
 
M

Martin Ambuhl

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.one, aa.one);

printf("aa[%u].two = %p\n", i, (void *) aa.two);
printf("aa[%u].three = %p\n", i, (void *) aa.three);
for (j = 0; j < 2; j++) {
printf("aa[%u].two[%u] = %p, points to \"%s\"\n", i, j,
(void *) aa.two[j], aa.two[j]);
printf("aa[%u].three[%u] = %p, points to \"%s\"\n", i, j,
(void *) aa.three[j], aa.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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top