On Wed, 30 Jun 2004 12:18:51 GMT,
rlb@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
[color=blue]
>Darklight <nglennglen@netscape.net> wrote:
>[color=green]
>> What is written below i will use as a quick reference i just want to make
>> sure i have got it right
>> /* STRUCTURES */
>>
>> TO DEFINE A STRUCTURE
>> way 1. To declare a structure and instance/s together.
>> struct coord{ /* struct keyword is used to declare a stucture, coord
>> is the structure name or tag */
>> int x;
>> int y; /* x & y are the names of the member variables of the
>> structure */
>> } first, second; /* first & second are the instances of the structure */
>> /* each instance contains two integers */[/color]
>
>Yes.
>[color=green]
>> WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
>> struct coord{
>> int x;
>> int y;
>> };
>> /* additional code goes here */
>> struct coord first, second;[/color]
>
>Yes.
>[color=green]
>> ACCESSING STRUCTURE MEMBERS.
>> first.x = 50;
>> first.y = 100; /* to access a structure member you have to use the
>> dot (.) operator between */
>> /* the structure name and the member name */[/color]
>
>Yes.
>[color=green]
>> WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
>> struct time{
>> int hours;
>> int minutes;
>> int seconds;
>> }time_of_birth = {8,45,0};[/color]
>
>Yes.
>[color=green]
>> TO COPY INFORMATION BETWEEN STRUCTURES.
>> first = second;
>> first = second;[/color]
>
>You only need to do it once...
>[color=green]
>> the above is equivalent to
>> first.x = second.x;
>> first.y = second.y;[/color]
>
>Not necessarily. It may also be equivalent to
>
> memcpy(&first, &second, sizeof first);
>
>or, indeed, even stranger code. All you know is that after the copy, the
>values of the structure members are identical; the content of any
>padding bytes remains unspecified.
>[color=green]
>> STRUCTURES THAT CONTAIN ARRAYS
>> struct data{
>> int x[4];
>> char y[10]
>> }record;
>>
>> record.y /* IS A POINTER TO THE FIRST ELEMENT OF THE ARRAY */[/color]
>
>No. It is the array. It has array type.[/color]
True. However, in most expressions other than as the operand of the
sizeof or & operators, it evaluates to the address of the first
element with type pointer to element.
[color=blue]
>[color=green]
>> ARRAYS OF STRUCTURES
>> struct entry{
>> char fname[10];
>> char lname[12];
>> char phone[8];
>> };
>> struct entry list[100]; /* declaring an array of structures of type entry */
>> /* each element is a structure of type entry */
>> /* each element of list array has three elements */
>> /* IE: first element list[0] = list[0].fname,
>> list[0].lname, list[0].phone */
>> /* last element list[999] = list[999].fname,
>> list[999].lname, list[999].phoone */[/color]
>
>Yes.
>[color=green]
>> INITIALIZING STRUCTURES
>>
>> struct customer{
>> char firm[20];
>> char contact[25];
>> };
>>
>> struct sale{
>> struct customer buyer; /* structure to a structure */
>> char item[20];
>> float amount;
>> };
>>
>> struct sale y1990[100] = { /* initializing the structures */
>> {{ "acme industries", "George Adams"},
>> "left-hand widget",
>> 1000.00
>> },
>> {{ "Wilson & co.","Ed Wilson"},
>> "Type 12 gizmo",
>> 200.00
>> }
>> };
>>
>> TO DISPLAY THE ABOVE STRUCTURES
>>
>> for(i = 0; i < 2; i++)
>> {
>> printf("%s %s\n %s £%.2f\n\n", y1990[i].buyer.firm,
>> y1990[i].buyer.contact, y1990[i].item, y1990[i].amount);
>> }[/color]
>
>Yes.
>[color=green]
>> POINTERS AS STRUCTURE MEMBERS
>> struct data{
>> int *value;
>> int *rate
>> }first; /* You must initialize pointers to point at some
>> thing */
>> /* to do this you do as below */
>>
>> first.value = &cost;
>> first.rate = &interest; /* cost and interest must have been declared as int variables */[/color]
>
>Not necessarily. You could also use malloc(); or copy existing pointers
>to them; or, where appropriate, set them to null pointers.
>[color=green]
>> /* When using char variable do not use the &
>> operator */[/color]
>
>Nope. When pointing a char * member at a _string_, do not use &. When
>pointing it at a single char, you still need the &.
>
>[color=green]
>> POINTERS TO STRUCTURES
>> struct part{ /* define a structure */
>> int number;
>> char name[10];
>> };
>> struct part *p_part; /* declare a pointer to type part;
>> struct part gizmo; /* declare an instance to type part */
>> p_part = &gizmo; /* perform pointer initialization */
>>
>> (*p_part).number = 100; /* this assigns the value 100 to
>> gizmo.number*/
>> /* this uses the indirection operator (*)*/
>> p_part->number /* this is used to access structure members
>> using a pointer */
>> /* this uses the indirect membership operator
>> (->) */[/color]
>
>Note that those two are identical in all regards except spelling.
>[color=green]
>> POINTERS AND ARRAYS OF STRUCTURES
>> struct part{ /* define struture */
>> int number;
>> char name[10];
>> };
>>
>> struct part data[100]; /* declare an array of type part */
>> struct part *p_part; /* declare a pointer to type part */
>> p_part = &data[0]; /* initialize pointer to point to first element in array */
>> p_part = data; /* second way to do above */[/color]
>
>Yes. Note lack of & operator in the second initialisation!
>[color=green]
>> printf("%s %s", p_part->number, p_part->name);
>> /* to print contents of first element */[/color]
>
>No. That should be
>
> printf("%d %s", p_part->number, p_part->name);
>
>or something similar; p_part->number is an int, not a char *.
>[color=green]
>> PASSING STRUCTURES AS ARGUMENTS TO FUNCTIONS
>> struct data{ /* define and initialize a structure */
>> float amount;
>> char fname[30];
>> char lname[30];
>> }rec;
>>
>> void print_rec(struct data x); /* declare function prototype*/
>> /* addditional statements go here */
>> print_rec(rec); /* call function */[/color]
>
>Yes.
>
>Richard[/color]
<<Remove the del for email>>