473,226 Members | 1,528 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,226 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 3951
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, Jose 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;
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.