Another c struct syntax question

Y

Yusuf

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;
}
 
J

Jack Klein

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;
}
 
G

Guest

Yusuf said:
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 } },
};
 
H

Hubble

Yusuf said:
//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
 
K

kondal

Yusuf said:
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
 
Y

Yusuf

Thanks very much for taking the time to post back in this thread
everyone.
Regards
Yusuf
 
Y

Yusuf

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;
}
 
G

Guest

Yusuf said:
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.
 
Y

Yusuf

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;
}
 
G

Guest

Yusuf said:
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.)
 
Y

Yusuf

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
 
K

Keith Thompson

kondal said:
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.
 
K

Keith Thompson

Yusuf said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,194
Latest member
KarriWhitt

Latest Threads

Top