473,769 Members | 6,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another c struct syntax question

I'm sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can't seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}} ?

I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>

struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};

struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};

//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];

int main(void)
{
test3[0].testint=1;
int *testArray=mall oc(5*sizeof(int ));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=t estArray;
return 0;
}

Sep 23 '06 #1
12 3996
On 23 Sep 2006 09:14:34 -0700, "Yusuf" <as**********@h otmail.com>
wrote in comp.lang.c:
I'm sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can't seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}} ?

I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>

struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};

struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};

//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.
You can't initialize pointers this way, especially not at constant
scope, with the exception of pointers to char which can be initialized
with a string literal. What you can do is this:

int array1 [] = { 1, 3, 4, 7, 7 };
int array2 [] = { 3, 5, 7, 9, 12 };
int array3 [] = { 2, 4, 6, 8, 10, 12, 14 };
struct teststruct3
{
int testint;
int *testintArray;
};
struct teststruct3 test3 [3] =
{
{ sizeof array1 / sizeof *array1, array1 };
{ sizeof array2 / sizeof *array2, array2 };
{ sizeof array3 / sizeof *array3, array3 };
};

Note that in my experience most experienced C programmers frown on
defining a type and an object or array of that type in the same source
construct. Let alone initializing.

The way I have done it adds some extra white space to the source,
which is completely free, and an extra "struct teststruct3", cheap,
and an extra ';', very very cheap.
int main(void)
{
test3[0].testint=1;
int *testArray=mall oc(5*sizeof(int ));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=t estArray;
return 0;
}
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Sep 23 '06 #2
Yusuf wrote:
I'm sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can't seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}} ?
In C89, you cannot. In C99, you can. It's very well possible that
whatever compiler you are using does not support C99, though.

[...]
struct teststruct3
{
int testint;
int *testintArray;
} test3[3]
= {
{ 1, (int []) { 1, 2, 3 } },
{ 2, (int []) { 2, 4, 6 } },
{ 3, (int []) { 3, 6, 9 } },
};

Sep 23 '06 #3
Yusuf schrieb:
//I want to initialise test3 ...
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];

int main(void)
{
test3[0].testint=1;
int *testArray=mall oc(5*sizeof(int ));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=t estArray;
return 0;
}
You can use an additional "helper" arrays:

int testintArray0[5]={ 1, 2, 4, 7, 7 };
int testintArray1[5]={ 1, 1, 1, 1, 1 };
int testintArray2[5]={ 1, 2, 3, 4, 5 };
struct teststruct3
{
int testint;
int *testintArray;
} test3[3]=
{ { 1, testintArray0 },
{ 2, testintArray1 },
{ 3, testintArray2 }
};

Hubble

Sep 23 '06 #4

Yusuf wrote:
I'm sorry for the second post in as many hours, and both about structs.

The code below compiles. The variables test1 and test2 are structures
of datatype teststruct1 and teststruct2. The size of testintArray is
explictly set to 2 and 3 in teststruct1 and teststruct2. I want to
define a generic structure so that testintArray points to an array of
ints. The generic structure I have written is teststruct3.

What I can't seem to figure out is the syntax how to initialise test3
with data like I have done for test2 or test1 with syntax {1,{1,2,3}}.
I have had to do it a long way with malloc etc in function main. Can I
initialise the test3 instance of the struct teststring3 using a syntax
like{1,{1,2,3}} ?

I would appreciate help with the syntax. Thank you.
Yusuf
#include <stdlib.h>

struct teststruct1
{
int testint;
int testintArray[2];
} test1[3] =
{
{1,{1,2}},
{2,{1,9}},
{3,{2,9}}
};

struct teststruct2
{
int testint;
int testintArray[3];
} test2[3] =
{
{1,{1,2,3}},
{2,{1,4,5}},
{3,{2,3,1}}
};

//I want to initialise test3 like I have
//done with test2 and test1 above using the
//easy to read {1,{1,2,3}} syntax or something
//like it.
struct teststruct3
{
int testint;
int *testintArray;
} test3[3];

int main(void)
{
test3[0].testint=1;
int *testArray=mall oc(5*sizeof(int ));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=t estArray;
return 0;
}
If you are using another array to initialize your structures, why don't
you use memcpy.

Create a integer pointer to the structure and memcpy the entire
structure. If not you can use it to initialize individually.

-kondal

Sep 24 '06 #5
Thanks very much for taking the time to post back in this thread
everyone.
Regards
Yusuf

Sep 24 '06 #6
Thanks for this Harald. I am using a c99 flag in gcc so the syntax is
fine.

What I want to do using this syntax is initialise teststruct4. In my
code teststruct4a is legal and the compiler does not complain. But the
compiler does complain about teststruct4b. The error is:

t2.c:31: error: 'teststruct3' undeclared here (not in a function)
t2.c:32: error: expected '}' before '{' token
t2.c:37: warning: excess elements in struct initializer
t2.c:37: warning: (near initialization for 'test4b[0]')

I want to initialise teststruct4b inline as I have done, through your
help, with teststruct3. I haven't got the syntax for teststruct4b
correct. My code is below.
Thank you to everyone in this group for giving up their time helping me
learn c.
Yusuf

struct teststruct3
{
int testint;
int *testintArray;
};

struct teststruct3 test3[3] =
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
};
struct teststruct4a
{
char *testchar;
struct teststruct3 *teststruct;
} test4a[1] =
{
{"testchar",tes t3}
};
struct teststruct4b
{
char *testchar;
struct teststruct3 *teststruct;
} test4b[1] =
{
{"testchar",(te ststruct3)
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
}
}
};
int main(void)
{
return 0;
}

Sep 24 '06 #7
Yusuf wrote:
struct teststruct4b
{
char *testchar;
struct teststruct3 *teststruct;
} test4b[1] =
{
{"testchar",(te ststruct3)
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
}
}
};
You haven't defined any typedef named teststruct3. You have defined a
struct type named teststruct3, but you can only refer to that as
"struct teststruct3". And you want more than one instance of that
struct, but you only ask for one.

(struct teststruct3 []) { ... } should work.

Sep 24 '06 #8
Thank you Harald for the very fast reply. It does work now!

As you might have guessed, I am not doing this for a specific program,
I am doing this to learn c. Just to follow-up the alternate typedef
approach you mentioned and how I hadn't defined any typedef named
teststruct3, in my sample code below I have modified it so I now have
so I can look at this too. But the compiler is now complaining because
my code is wrong again. Again I've tried lots of syntax but I just
don't have the experience yet. Could you advise on what the problem is
here with teststruct4b now (note teststruct3 is now typedef). The error
is:

t2.c:26: warning: initialization from incompatible pointer type
Code below.
Thank you again
Yusaf
typedef struct
{
int testint;
int *testintArray;
} teststruct3;

struct teststruct4a
{
char *testchar;
struct teststruct3 *teststruct;
};

struct teststruct4b
{
char *testchar;
struct teststruct3 *teststruct;
} test4b[1] =
{
{"testchar",(te ststruct3[])
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
}
}
};

int main(void)
{
return 0;
}

Sep 24 '06 #9
Yusuf wrote:
Thank you Harald for the very fast reply. It does work now!

As you might have guessed, I am not doing this for a specific program,
I am doing this to learn c. Just to follow-up the alternate typedef
approach you mentioned and how I hadn't defined any typedef named
teststruct3, in my sample code below I have modified it so I now have
so I can look at this too. But the compiler is now complaining because
my code is wrong again. Again I've tried lots of syntax but I just
don't have the experience yet. Could you advise on what the problem is
here with teststruct4b now (note teststruct3 is now typedef). The error
is:
The initialisation is correct, but this time, the declaration is valid
but different from what you intended.
t2.c:26: warning: initialization from incompatible pointer type
[...]
typedef struct
{
int testint;
int *testintArray;
} teststruct3;

struct teststruct4a
{
char *testchar;
struct teststruct3 *teststruct;
};
You're declaring a new type "struct teststruct3", which is different
from "teststruct 3". Try "teststruct 3 *teststruct" instead. (And do the
same for teststruct4b.)

Sep 24 '06 #10

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

Similar topics

19
9237
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As "sizeof(int)" is legal I assumed "sizeof(struct x.y)" to be legal too. But is is not: #include <dirent.h>
3
2178
by: Emanuele Blanco | last post by:
Hi there, I just compiled a program that uses linked lists (needed it as an homework for my Programming course at University). It works flawlessly, even if I notice a thing. Here's my linked list declaration: struct node { float item; struct node *next; };
19
2641
by: Russell Shaw | last post by:
Hi, I have two structs in a header file, and they reference each other, causing a compile error. Is there a standard way to deal with this? typedef struct { ... RtAction *actions; } RtWidget;
20
2129
by: ma0001yu | last post by:
Hi, all. I feel confuse about below struct definition: typedef struct TAG { comments.... }; What my confusion is: is typedef extra??why we not just use
14
2309
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef whateverkeithsaysfor99compliance #define whateverkeithsays for99compliance int main(void { struct foo
6
2265
by: fcvcnet | last post by:
Hi, I read the book C++ Primer, Fourth Edition By Stanley B. Lippman, Jos¨¦e Lajoie, Barbara E. Moo "If we define a class using the class keyword, then any members defined before the first access label are implicitly private; ifwe usethe struct keyword, then those members are public. Whether we define a class using the class keyword or the struct keyword affects only the default initial access level." Now I defined a struct with...
2
5216
by: berrylthird | last post by:
This question was inspired by scripting languages such as JavaScript. In JavaScript, I can access members of a class using array syntax as in the following example: var myInstance:myClass = new myClass(); myInstance.member_0 = memberValue_0; // absolute notation myInstance = memberValue_0; // relative notation myInstance = memberValue_0; // nominal notation You can initialize a struct in C/C++ like you would an array, as with the...
14
2796
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
4
9815
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
10222
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...
0
10050
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...
1
9999
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
9866
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
8876
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...
1
7413
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3570
muto222
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.