Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
08/08/01 21:10
Read: times


 
#13942 - RE: initialising values of structure
1) Using the Brooks method, can we initialise only the first 20 tests instead of all the 30 at a time?

You can if you remove the [MAX_TESTS] off of each structure member and instead make an array of structures.

Example:

struct EXAMPLE
{
char title[20];
int length;
int flag;
}

struct EXAMPLE exampleArray[] =
{
{"Title 1", 20, 15},
{"Title 2", 18, 23},
{"Title 3", 25, 18}
};

This is kind of slick in that you group everything having to do with "Title 1" together. 20 is the length and 15 is the flag. The above would assign the following values:

exampleArray[0].title = "Title 1"
exampleArray[0].length = 20
exampleArray[0].flag = 15
exampleArray[1].title = "Title 2"
exampleArray[1].length = 18
exampleArray[1].flag = 23
exampleArray[2].title = "Title 3"
exampleArray[2].length = 25
exampleArray[2].flag = 18

With your current approach you can do the same thing, but it's going to be kind of weird. You'd do it as follows:

struct EXAMPLE
{
char title[3][20];
int length[3];
int flag[3];
}

struct EXAMPLE exampleArray =
{
{"Title 1", "Title 2", "Title 3"},
{20, 18, 25},
{15, 23, 18},
};

As you can see, in the second method (your current method) it's kind of weird since you have to group all the titles together, all the lengths together, and all the flags together. It's not as easy to maintain or modify since all the information related to Title 1 isn't grouped together.

a) I want to initialise the values in another structure say tmpTestParam instead of saveTestParam and transfer the contents to this structure only when required.

You can do this with:

memcpy(&saveTestParam, &tmpTestParam, sizeof(tmpTestParam));

This will copy the entire tmpTestParam structure to the saveTextParam structure. This assumes both structures are of the same size and have the same definition.

b) How is the assignment of two structures implemented in 'c'? like saveTestParam = tmpTestParam

This is essentially what the memcpy() example just mentioned does.

How can I assign only first 15 tests of tmpTestParam to saveTestParam.

This is where it would be very useful to implement it as I mentioned in a previous post. If you implement it as an array of structureType you could just do:

memcpy(&destArray, &srcArray, 15 * sizeof(structureType));

If you implement as you have, with each element having a [MAX_TESTS] dimension, you'll have to copy the 15 tests in a for() loop, copying the values of each structure member one by one.

Craig Steiner

List of 6 messages in thread
TopicAuthorDate
initialising values of structure            01/01/70 00:00      
RE: initialising values of structure            01/01/70 00:00      
RE: initialising values of structure            01/01/70 00:00      
RE: initialising values of structure            01/01/70 00:00      
RE: initialising values of structure            01/01/70 00:00      
RE: initialising values of structure            01/01/70 00:00      

Back to Subject List