473,881 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

initialize an array of elements that contain another array

hi,
I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};

Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
{1, "first.firs t"},
20, "second",
{2, "second.second" }
};

compiler generates following similar error:
error: a value of type "1" cannot be used to initialize an entity of
type "array1 *"

How am I going to initialize something like above. Thx

Jul 31 '06 #1
10 3044
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
wenm...@yahoo.c om wrote:
hi,
I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};

Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
{1, "first.firs t"},
20, "second",
{2, "second.second" }
};

compiler generates following similar error:
error: a value of type "1" cannot be used to initialize an entity of
type "array1 *"

How am I going to initialize something like above. Thx
You are going to explicitly malloc() space for your array1 entries, and
populate the myArray pointers in array2 with the malloc()ed values.

You are going to do this in program logic.

You are *not* going to do this with an initializer.

HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEzltYagV FX4UWr64RAh+EAJ 0XihKh3M+dhmO3s Aad8tsmGutOFACg q/UR
bDbPZO+MoKcIVOG HvLxgTKA=
=bwDm
-----END PGP SIGNATURE-----

Jul 31 '06 #2
array1 *myArray;

Change to:

array1 myArray;

--

Frederick Gotham
Jul 31 '06 #3

Lew Pitcher wrote:
You are going to explicitly malloc() space for your array1 entries, and
populate the myArray pointers in array2 with the malloc()ed values.

You are going to do this in program logic.

You are *not* going to do this with an initializer.

HTH
- --
Lew Pitcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFEzltYagV FX4UWr64RAh+EAJ 0XihKh3M+dhmO3s Aad8tsmGutOFACg q/UR
bDbPZO+MoKcIVOG HvLxgTKA=
=bwDm
-----END PGP SIGNATURE-----

actually, I want an array1 inside array2 without specifying the size of
array1. The size of array1 is determined through initialization list,
can I do that?
struct array2
{
int id;
char *name;
array1 myArray[]; - will not compile here
};

Jul 31 '06 #4
struct type1
{
int id;
char *name;
};
struct type2
{
int id;
char * name;
type1 *myType1Collect ion; //should be arrary of type1 here: type1
myType1Collceti on[]?

};

The actual array size for type1 is a variable and can only determined
through initialization list after reading from external input through a
code generation routine. How can I achieve that?

Jul 31 '06 #5
struct type1
{
int id;
char *name;
};
struct type2
{
int id;
char * name;
type1 *myType1Collect ion; //should be arrary of type1 here: type1
myType1Collceti on[]?

};

The actual array size for type1 is a variable and can only determined
through initialization list after reading from external input through a
code generation routine. How can I achieve that?

Jul 31 '06 #6
we*****@yahoo.c om wrote:
hi,
I have following:
struct array1
{
int id;
char *name;
};
This is ok.
struct array2
{
int id;
char * name;
array1 *myArray;
};
The last member makes me worry. It is not an array but a pointer. Moreover,
you did not define the array1 type. If you are using a C++ compiler, this
is not a problem, but, well, in C, you'd better write
struct array1 *myArray;
But as already told, this is not an array. An array looks like
struct array1 myArray[5];

but then you fix the number of elements. You could also write
struct array1 myArray[];
but then you would like to define an array out ouf struct array2 which.
Should not all the members of this array be of the same type?
(Different sizes of myArrays make them incompatible.)

Szabolcs Borsanyi
Jul 31 '06 #7
we*****@yahoo.c om writes:
I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};

Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
{1, "first.firs t"},
20, "second",
{2, "second.second" }
};
The first thing you should do is stop compiling your code with a C++
compiler; either that, or ask in comp.lang.c++.

You haven't declared a type "array1", so the declaration of the third
element of your "struct array2" is illegal. You've declared a type
called "struct array1".

The following would be legal:

struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char *name;
struct array1 *myArray;
};

struct array2 collection[] = ...

[...]
How am I going to initialize something like above. Thx
C allows you to omit some braces in an initializer, but it's not
generally a good idea to take advantage of this. Enclosing the
initializers for each object and subobject in braces can make the code
easier to understand; it can also make it easier for the compiler to
catch some errors.

Assuming you want two elements in your array, you can declare it like
this:

struct array2 collection[] =
{ { 10, "first", ? },
{ 20, "second", ? } };

The question marks, of course, aren't legal; they need to be replaced
by something that initializes the myArray member of the struct array2
object.

What you want to do is initialize the myArray member (a pointer to
struct array1) so it points to an object of type struct array1. To do
this, you would need to allocate an object of type struct array1. You
can't really do this in an initializer.

Here's one possible approach:

struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char *name;
struct array1 *myArray;
};

struct array1 a1_0 = { 1, "first.firs t" };
struct array1 a1_1 = { 2, "second.sec ond" };

struct array2 collection[] =
{ { 10, "first", &a1_0 },
{ 20, "second", &a1_1 } };

Incidentally, "array1" and "array2" aren't very good names for
structures. I hope you're using something clearer in your real code.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 31 '06 #8
On 31 Jul 2006 12:28:16 -0700, we*****@yahoo.c om wrote:
>hi,
I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};
array2 has three members.
>
Now, I try to create an array of array2 by initialization:
array2 collection[] =
{
10, "first",
By omitting the braces on this line, you are telling the compiler that
this is not the complete list of initialization values for the first
element of the array. Since there are two values in this list, they
are assigned to id and name. The next value will be assigned to
myArray (which has type pointer to struct).

{1, "first.firs t"},
This is not a suitable value for myArray. If myArray were a struct
instead of a pointer to struct, it would probably work.
20, "second",
{2, "second.second" }
};

compiler generates following similar error:
error: a value of type "1" cannot be used to initialize an entity of
type "array1 *"

How am I going to initialize something like above. Thx
If you really want myArray to be a pointer, then you will need to
define a number of objects of type struct array1 (probably initialized
with the braced values above) and replace the braced initialization
values above with the address of the appropriate object (e.g.,
&array1_1).
Remove del for email
Aug 1 '06 #9
we*****@yahoo.c om wrote:
hi,
I have following:
struct array1
{
int id;
char *name;
};

struct array2
{
int id;
char * name;
array1 *myArray;
};
....
How am I going to initialize something like above. Thx
Hi Wenmang. (IIRC that means 'illiterate' in Chinese?)

Nobody here mentioned that you can use a feature from C99 to create an
automatic struct and take its address as part of the initialiser.

#include <stdio.h>

struct foo
{
int id;
char *name;
};

struct bar
{
int id;
char *name;
struct foo *pfoo;
};

int main(void)
{
struct bar myBar = {
10,
"hello",
& (struct foo) {20, "world"},
};
printf("{%d, %s, {%d, %s}}\n",
myBar.id, myBar.name,
myBar.pfoo->id, myBar.pfoo->name);
return 0;
}

Note that the unnamed struct object that was created has function scope.
If you do the initialisation inside a function and then return from the
function, the pointer will become invalid.

--
Simon.
Aug 2 '06 #10

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

Similar topics

2
4915
by: Dave Smithz | last post by:
Hi there. Because of the lack of a Union query in MySQL 3 I have decided to take the approach where I populate two arrays with values from similar tables in DB. In this case they are `courses` and `lessons` Lets say Courses has fields CourseID(PK), Date, Name, RunBy, Status The lessons table is related to the Courses table in that a Course can have
4
38262
by: Mark Hannon | last post by:
I am trying to initialize an array only once so it can be seen & used by any functions that need it. As I understand it, if a variable is declared by itself outside of any functions, its scope is global and any functions should be able to access it. I have been having trouble getting this to work. In the 1st of the 2 examples below I initialize the Array "initArray" with 4 form text fields. When I try to use initArray inside either of...
2
2104
by: Salman Khilji | last post by:
After reading all the FAQs, I cannot solve the following problem: I have a pointer of type double. I am supposed to 1) Allocate memory for it assuming that the pointer will be pointing to a multi-dimensional array whose dimensions will be specified at run-time. 2) Set the individual elements of the array using pointer-arithmetic.
15
5339
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have private string myArray;
22
26757
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
18
4227
by: toton | last post by:
Hi, In C++ when I initialize an array it, also initializes the class that it contains, which calls the default constructor. However, I want to initialize the array only (i.e reserve the space) and use my specific constructor to initialize the class. How to do it without using malloc? Something like Point* pt = new Point; I want it to reserve the space for N points only, and not to call default constructor. I dont have a default...
15
26445
by: thinktwice | last post by:
char a = { 0 } is it ok?
3
24362
by: Rode | last post by:
Hi, I want to initialize some elements in an array at the beginning(say 300 elements) and at the ending(230 elements) to 0 and the elements between start and end to a value based on the calculation. Currently, I am doing in the following way: float fArray; int i; for( i = 0; i < 300; i++) { fArray = 0.0; }
16
2212
by: sophia.agnes | last post by:
Hi, is there any guarantee that elements of an un initialized array by default contains the element 0 ?
0
9928
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
11100
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10816
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10401
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
9554
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
7110
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();...
1
4597
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
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.