473,472 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

strange structure initialization

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?

Nov 14 '05 #1
4 1624
dd@conus.info wrote on 20/03/05 :
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"

Nov 14 '05 #2
dd
No-no. There're will be unknown number of pointers, not 2, maybe even
zero.

Nov 14 '05 #3
dd@conus.info wrote on 20/03/05 :
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"

Nov 14 '05 #4
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"
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Skybuck Flying | last post by:
Hello, What does Const mean in this c structure ? and what is the delphi equivalent ? I think const struct just means it can't be modified... is that correct ? Struct { Type1 Field1;...
2
by: kimimaro | last post by:
hi I wonder if array can be work along with structure? Below are the declaration of my structure struct employee{ char ID; char Name; char Department;
10
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no...
5
by: aarklon | last post by:
Hi all, why the following structure initialization is not valid #include<stdio.h> struct rec { char name; int age; };
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
23
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even...
17
by: jb.simon | last post by:
Recently I was pinged in a code review about my use of the initialization method AStruct myStruct = { 0 } ; Which initializes all elements of the myStruct to 0. I was questioned on it because...
14
by: Bill Reid | last post by:
OK, let's say that have a function menu structure declaration like this (since I basically do): typedef struct function_item { char *descrip; void(*function)(void); } t_function_item; ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.