473,403 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
return 0;
}

Sep 23 '06 #1
12 3960
On 23 Sep 2006 09:14:34 -0700, "Yusuf" <as**********@hotmail.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=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
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.learn.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=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
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=malloc(5*sizeof(int));
testArray[0]=1;
testArray[1]=2;
testArray[2]=4;
testArray[3]=7;
testArray[4]=7;
test3[0].testintArray=testArray;
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",test3}
};
struct teststruct4b
{
char *testchar;
struct teststruct3 *teststruct;
} test4b[1] =
{
{"testchar",(teststruct3)
{
{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",(teststruct3)
{
{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",(teststruct3[])
{
{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 "teststruct3". Try "teststruct3 *teststruct" instead. (And do the
same for teststruct4b.)

Sep 24 '06 #10
Thank you again Harald. This works too! I have really got to sit down
and study this code now and convince myself I really do understand it.
Regards
Yusuf

Sep 24 '06 #11
"kondal" <ko******@gmail.comwrites:
[...]
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.
What do you mean by "integer pointer"? Do you mean just "pointer"?

It's not necessary to use memcpy() to copy structures; you can just
use an assignment.

--
Keith Thompson (The_Other_Keith) 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.
Sep 24 '06 #12
"Yusuf" <as**********@hotmail.comwrites:
[...]
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",(teststruct3[])
{
{1, (int []) { 1, 2, 3 } },
{2, (int []) { 3, 4, 5 } },
{3, (int []) { 6, 7, 8 } }
}
}
};

int main(void)
{
return 0;
}
For the sake of consistency, don't use typedefs. You originally
declared your "teststruct3" type as "struct teststruct3 { ... };";
there was no reason to change that to a typedef. The problem was
that, later on, you referred to the type as "teststruct3". You just
need to refer to it as "struct teststruct3".

A typedef for a struct type merely declares another name for something
that already has one. Some people like being able to refer to the
type with a single identifier, but it's not at all necessary.

(If you're going to use typedefs, you should be consistent; having
"teststruct3" as a typedef name and "teststruct4b" as a struct tag is
just confusing.)

--
Keith Thompson (The_Other_Keith) 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.
Sep 24 '06 #13

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

Similar topics

19
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...
3
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...
19
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; }...
20
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
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...
6
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...
2
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...
14
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
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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,...
0
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...

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.