Initializing an array of structures
-
struct _X
{
int field1;
int field2;
int field3;
} anArray[15];for (int i = 0; i < 15; ++i)
{
anArray[i].field1 = 1;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field2 = 2;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field3 = 3;
} -
struct _X
{
int field1;
int field2;
int field3;
} anArray[15];for (int i = 0; i < 15; ++i)
{
anArray[i].field1 = 1;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field2 = 2;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field3 = 3;
}How horrible this is depends on the language: is it C? C++? something else? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
How horrible this is depends on the language: is it C? C++? something else? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
It's C, but I don't see how the language makes it any better or worse.
-
It's C, but I don't see how the language makes it any better or worse.
some languages such as C# allow for code inside a struct; C does not. How would you do it in C? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
some languages such as C# allow for code inside a struct; C does not. How would you do it in C? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software. -
No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.right. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.Exactly the point I was trying to make.
-
struct _X
{
int field1;
int field2;
int field3;
} anArray[15];for (int i = 0; i < 15; ++i)
{
anArray[i].field1 = 1;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field2 = 2;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field3 = 3;
} -
I could find better ways.
-
I could find better ways.
glad to see you're back. :-D
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
glad to see you're back. :-D
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Was just in Vegas, couldn't be bothered to find a wifi hot spot.
-
struct _X
{
int field1;
int field2;
int field3;
} anArray[15];for (int i = 0; i < 15; ++i)
{
anArray[i].field1 = 1;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field2 = 2;
}for (int i = 0; i < 15; ++i)
{
anArray[i].field3 = 3;
}Really horrible, poor guy didn't consider performance of such code. I have far more superior solution:
anArray[0].field1 = 1;
anArray[1].field1 = 1;
anArray[2].field1 = 1;
anArray[3].field1 = 1;
anArray[4].field1 = 1;
anArray[5].field1 = 1;
anArray[6].field1 = 1;
anArray[7].field1 = 1;
anArray[8].field1 = 1;
anArray[9].field1 = 1;
anArray[10].field1 = 1;
anArray[11].field1 = 1;
anArray[12].field1 = 1;
anArray[13].field1 = 1;
anArray[14].field1 = 1;anArray[0].field2 = 2;
anArray[1].field2 = 2;
anArray[2].field2 = 2;
anArray[3].field2 = 2;
anArray[4].field2 = 2;
anArray[5].field2 = 2;
anArray[6].field2 = 2;
anArray[7].field2 = 2;
anArray[8].field2 = 2;
anArray[9].field2 = 2;
anArray[10].field2 = 2;
anArray[11].field2 = 2;
anArray[12].field2 = 2;
anArray[13].field2 = 2;
anArray[14].field2 = 2;anArray[0].field3 = 3;
anArray[1].field3 = 3;
anArray[2].field3 = 3;
anArray[3].field3 = 3;
anArray[4].field3 = 3;
anArray[5].field3 = 3;
anArray[6].field3 = 3;
anArray[7].field3 = 3;
anArray[8].field3 = 3;
anArray[9].field3 = 3;
anArray[10].field3 = 3;
anArray[11].field3 = 3;
anArray[12].field3 = 3;
anArray[13].field3 = 3;
anArray[14].field3 = 3; -
No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.I don't know, maybe Brain fuck doesn't. :laugh: Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
-
Really horrible, poor guy didn't consider performance of such code. I have far more superior solution:
anArray[0].field1 = 1;
anArray[1].field1 = 1;
anArray[2].field1 = 1;
anArray[3].field1 = 1;
anArray[4].field1 = 1;
anArray[5].field1 = 1;
anArray[6].field1 = 1;
anArray[7].field1 = 1;
anArray[8].field1 = 1;
anArray[9].field1 = 1;
anArray[10].field1 = 1;
anArray[11].field1 = 1;
anArray[12].field1 = 1;
anArray[13].field1 = 1;
anArray[14].field1 = 1;anArray[0].field2 = 2;
anArray[1].field2 = 2;
anArray[2].field2 = 2;
anArray[3].field2 = 2;
anArray[4].field2 = 2;
anArray[5].field2 = 2;
anArray[6].field2 = 2;
anArray[7].field2 = 2;
anArray[8].field2 = 2;
anArray[9].field2 = 2;
anArray[10].field2 = 2;
anArray[11].field2 = 2;
anArray[12].field2 = 2;
anArray[13].field2 = 2;
anArray[14].field2 = 2;anArray[0].field3 = 3;
anArray[1].field3 = 3;
anArray[2].field3 = 3;
anArray[3].field3 = 3;
anArray[4].field3 = 3;
anArray[5].field3 = 3;
anArray[6].field3 = 3;
anArray[7].field3 = 3;
anArray[8].field3 = 3;
anArray[9].field3 = 3;
anArray[10].field3 = 3;
anArray[11].field3 = 3;
anArray[12].field3 = 3;
anArray[13].field3 = 3;
anArray[14].field3 = 3; -
I don't know, maybe Brain fuck doesn't. :laugh: Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
-
some languages such as C# allow for code inside a struct; C does not. How would you do it in C? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
that is something different, it generates no run-time code. It may or may not be sufficient for what the original program needed, assuming you duplicate the constants 15 times. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Can't modify a deleted message :P I couldn't make it scalable so I deleted my post:
struct k[15] = { {1,2,3},...}
bulg wrote:
Can't modify a deleted message
No, however you can edit a message instead of deleting it. :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
No matter what language - all of them allow at least for summarizing this in one loop construct... :) Regards Thomas
www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Programmer - an organism that turns coffee into software.I especially love the brackets he put around the loop bodies - in case he needs to add some additional instructions there!