473,670 Members | 2,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initialize pointer-to-struct declaration to an unnamed struct?

Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I'm only concerned with static/global variables, if it matters.)

I'm trying to do something like:

struct st_a {
int i, j;
};

struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
};

It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it's also more difficult to read when the
idea is a hierarchy.

Jun 26 '07 #1
18 9109
Ehud Shapira wrote:
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I'm only concerned with static/global variables, if it matters.)

I'm trying to do something like:

struct st_a {
int i, j;
};

struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
{0, 1} is not a valid initializer for a pointer.
};

It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it's also more difficult to read when the
idea is a hierarchy.
It's not quite clear to me what you actually want.

Jun 26 '07 #2
{0, 1} is not a valid initializer for a pointer.
I know. What *can* be valid? You can see what sort of initialization
I'm trying to do.

Jun 26 '07 #3
Ehud Shapira wrote:
>{0, 1} is not a valid initializer for a pointer.
I know. What *can* be valid? You can see what sort of initialization
I'm trying to do.
Actually, I can't. Why does it have to be a pointer? Can't you just store a
struct directly?

Jun 26 '07 #4
On Jun 26, 11:17 pm, Rolf Magnus <ramag...@t-online.dewrote:
Actually, I can't. Why does it have to be a pointer? Can't you just store a
struct directly?
What I want to do involves a hierarchy of variable length struct
arrays. But regardless of the why, the question is whether it's
possible at all to do something like ( char *pc = "something" ) for a
struct and not char.

Jun 26 '07 #5
Ehud Shapira wrote:
On Jun 26, 11:17 pm, Rolf Magnus <ramag...@t-online.dewrote:
>Actually, I can't. Why does it have to be a pointer? Can't you just
store a struct directly?
What I want to do involves a hierarchy of variable length struct
arrays. But regardless of the why, the question is whether it's
possible at all to do something like ( char *pc = "something" ) for a
struct and not char.
Usually not. There is a huge difference between a string literal (that
has a very very very long lifetime) and a struct temporary. You could
create a static object of the struct type, and then declare a pointer
and initialise it with the address of the static object:

static somestruct something = { blah };
somestruct *ps = &something;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 26 '07 #6
>There is a huge difference between a string literal and a struct temporary.
The struct is just as static -- it's a global definition.
You could create a static object of the struct type, and then declare a pointer
and initialise it with the address of the static object:
That's what I'm trying to avoid; naming every linked array of structs.

Jun 26 '07 #7
Ehud Shapira wrote:
>There is a huge difference between a string literal and a struct
temporary.
The struct is just as static -- it's a global definition.
No, a temporary created for the purposes of initialising something is
definitely not static. It's temporary.
>You could create a static object of the struct type, and then
declare a pointer and initialise it with the address of the static
object:
That's what I'm trying to avoid; naming every linked array of structs.
<shrug Don't name them. Have an array and get pointers to its
elements:

struct st_a {
int i, j;
} a[] = { { 0, 1 }, {0, 2 } };

struct st_b {
int k, l;
st_a *m;
} b[] = {
{ 1, 2, a+0 },
{ 3, 4, a+1 }
};

You won't have "every" element named, only one.

Another solution is to create those dynamically:

struct st_a {
int i, j;
};

st_a* make_st_a(int i, j) {
st_a a = {i, j};
return new st_a(a);
}

struct st_b {
int k, l;
st_a *m;
} b[] = {
1, 2, make_st_a(0, 1),
3, 4, make_st_a(0, 2)
};

If 'b' is static, you don't have to care about disposing of
those dynamic objects, a small leak like that isn't going to
hurt much. Just be aware of it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #8
On Jun 26, 8:52 pm, Ehud Shapira <ehudshap...@ho tmail.comwrote:
Is it possible to have a declaration of a struct pointer initialized
to an unnamed struct?
(I'm only concerned with static/global variables, if it matters.)
I'm trying to do something like:
struct st_a {
int i, j;
};
struct st_b {
int k, l;
st_a *m;} b[] = {
1, 2, {0, 1},
3, 4, {0, 2}
};
It can't be done in C++. In C you can write something like:

typedef struct
{
int i, j ;
} st_a ;

typedef struct
{
int k, l ;
st_a* m ;
} st_b ;

st_b b[] = {
{ 1, 2, &(st_a){ 0, 1 }, },
{ 3, 4, &(st_a){ 0, 2 }, },
} ;

but this was added to C after C++ was standardized. (Possibly
some C++ compilers support it anyway. I don't know.)
It could be done by declaring each "sub-struct" with a name, then
pointing to these,
but besides being more work it's also more difficult to read when the
idea is a hierarchy.
In such cases, I usually define the data in a simple text file,
and then write a small program to convert it into legal C++.

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '07 #9
eh*********@gma il.com wrote:
On Jun 27, 2:33 am, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>>The struct is just as static -- it's a global definition.
No, a temporary created for the purposes of initialising something is
definitely not static. It's temporary.

I don't understand that. Maybe we are talking about different things?
The struct will be initialized at compile-time, and so will live
"forever". How is that temporary?
In the statement

T obj = someexpression;

(provided it appears at the namespace scope) 'obj' has static storage
duration, but whatever 'someexpression ' returns does NOT have static
storage duration, it's a temporary. That's significantly different
from

char const* p = "blah";

where the string literal ("blah") has static storage duration.
>
><shrug Don't name them. Have an array and get pointers to its
elements:

Bookkeeping array indexes isn't much better. And dynamically creating
them would add overhead, etc.

On Jun 27, 12:03 pm, James Kanze <james.ka...@gm ail.comwrote:
>In such cases, I usually define the data in a simple text file,
and then write a small program to convert it into legal C++.

Yeah, if that's the case, for big definitions I'll probably go with a
custom preprocessor, possibly outputting binary directly (which would
also rid me of unclear handling of union initialization by VC6... but
maybe that's the problem; using an old compiler.)

Too bad C/++ doesn't offer (in many cases) such terseness as that
possible by, say, JavaScript.
Huh? Terseness?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 27 '07 #10

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

Similar topics

0
3700
by: Mario S. | last post by:
Hi, how can I determine, if the XMLPlatformUtils::Initialize() was already called or not ? There is no static method XMLPlatformUtils::isInitialized(). Perhaps this would be a nice feature in the next Xerces Release ? For now, I have a little workaround. Because it is really necessary that a TransService is loaded and XMLPlatformUtils::fgTransService is in any case not NULL after Initialize() method was called, I use this behaviour and
4
2295
by: Avner Flesch | last post by:
Hi, Do you know how can I intialize an array member: for example class A { public: A(int x); };
3
2085
by: Tony Johansson | last post by:
Hello!! Hello Experts! I'm right if I say that if I have attribute that is constant or attribute that is of reference type or attribute that is class type then I must use initialize list. //Tony
20
6554
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some area of ram that's not reserved by the program. Accessing memory through such pointer is likely to result in core dump (memory access violation)"
2
2539
by: K. Culleton | last post by:
Don't install Windows XP SP2 if you use db2cctr.exe and have DB2 V7.1.0.6.0 installed. After I installed XP SP2, db2cctr.exe would not initialize. After I uninstalled SP2, db2cctr.exe worked fine. KC
0
3430
by: Bruno Rodrigues | last post by:
Hi, I have a Data Access class with a .startBanco() method, and all my other class libraries access this one. Windows Form (GUI) Class Library (Bussiness Logic) Class Library (Data Access) Can I fire .startBanco() when I initialize (instance) my second class,
0
911
by: Bryan | last post by:
I'm using the UIP App block and I have overridden the Initialize method, but my override never gets called. Here's the code: public override void Initialize(TaskArgumentsHolder args, ViewSettings settings) { base.Initialize (args, settings); Logger.Write("Initialize");
8
1998
by: bryan | last post by:
I'm using the UIP App block and I have overridden the Initialize method, but my override never gets called. Here's the code: public override void Initialize(TaskArgumentsHolder args, ViewSettings settings) { base.Initialize (args, settings); Logger.Write("Initialize");
21
7914
by: Howard Kaikow | last post by:
Is there documentation of what we should not change in the Initialize Component of a Windows form? For example, the following code was generated by VB .NET: Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(242, 223) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnGreeting, Me.txtName, Me.lblInstruction}) Me.Menu = Me.OptionMenu Me.Name = "frmHello"
12
5805
by: NewToCPP | last post by:
does the default constructor initialize values? I have a class as defined below: class A { int i; char c; int * iPtr;
0
8471
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8388
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8817
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8663
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7423
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5687
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4396
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2804
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 we have to send another system

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.