is this correct

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Darklight

    #1

    is this correct

    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 */

    WAY 2. TO DECLARE INSTANCE IN A DIFFERENT LOCATION
    struct coord{
    int x;
    int y;
    };
    /* additional code goes here */
    struct coord first, second;

    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 */

    WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
    struct time{
    int hours;
    int minutes;
    int seconds;
    }time_of_birth = {8,45,0};

    TO COPY INFORMATION BETWEEN STRUCTURES.
    first = second;
    first = second;

    the above is equivalent to
    first.x = second.x;
    first.y = second.y;

    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 */

    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 */

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

    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 */
    /* When using char variable do not use the &
    operator */

    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).numbe r = 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
    (->) */

    THREE WAYS TO ACCESS A STUCTURE MEMBER
    str.memb;
    (*p_str).memb;
    p_str->memb;

    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 */

    printf("%s %s", p_part->number, p_part->name);
    /* to print contents of first element */

    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(struc t data x); /* declare function prototype*/
    /* addditional statements go here */
    print_rec(rec); /* call function */

    void print_rec(struc t data x) /* declare or define function definition */
    {
    printf("\nDoner s name %s %s gave £%.2f\n",x.fna me,
    x.lname,x.amoun t);
    }
  • Richard Bos

    #2
    Re: is this correct

    Darklight <nglennglen@net scape.net> wrote:
    [color=blue]
    > 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=blue]
    > 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=blue]
    > 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=blue]
    > 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=blue]
    > TO COPY INFORMATION BETWEEN STRUCTURES.
    > first = second;
    > first = second;[/color]

    You only need to do it once...
    [color=blue]
    > 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=blue]
    > 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=blue]
    > 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=blue]
    > 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=blue]
    > 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=blue]
    > /* 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=blue]
    > 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).numbe r = 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=blue]
    > 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=blue]
    > 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=blue]
    > 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(struc t data x); /* declare function prototype*/
    > /* addditional statements go here */
    > print_rec(rec); /* call function */[/color]

    Yes.

    Richard

    Comment

    • pete

      #3
      Re: is this correct

      Darklight wrote:
      [color=blue]
      > WAY 3. TO DEDCLARE AND INITIALIZE A STRUCTURE.
      > struct time{
      > int hours;
      > int minutes;
      > int seconds;
      > }time_of_birth = {8,45,0};[/color]

      Could also be {8, 45} instead.

      --
      pete

      Comment

      • Al Bowers

        #4
        Re: is this correct



        Darklight wrote:[color=blue]
        > What is written below i will use as a quick reference i just want to make
        > sure i have got it right[/color]

        Bos has made a fine analysis of your quick reference sheet. I would
        suggest that you make an additional entry. Along with passing
        structures as arguments to funcions you should include:
        Passing sructure pointer as argument to functions. Instead of passing
        a copy of the structure it would probably be better to pass the
        pointer to the structure. This would be especially useful if the
        function is going to modify the structure. See example below.

        [color=blue]
        > 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(struc t data x); /* declare function prototype*/
        > /* addditional statements go here */
        > print_rec(rec); /* call function */
        >
        > void print_rec(struc t data x) /* declare or define function definition */
        > {
        > printf("\nDoner s name %s %s gave £%.2f\n",x.fna me,
        > x.lname,x.amoun t);
        > }[/color]

        PASSING STRUCTURE POINTER AS ARGUMENT TO FUNCTIONS
        Function definition:
        void print_rec(const struct data *x)
        {
        printf("\nDoner s name %s %s gave £%.2f\n",x->fname,
        x->lname,x->amount);
        return;
        }

        Function call:
        print_rec(&rec)

        Example:

        #include <string.h>
        #include <stdio.h>

        #define SZ_STR 30

        struct bio
        {
        char name[SZ_STR+1];
        unsigned age;
        };

        void setbio(struct bio *p, const char *name, unsigned age);
        void printbio(const struct bio *p);

        int main(void)
        {
        struct bio firstprez = {""}; /* no name, no age */

        printbio(&first prez);
        puts("\nNow setting bio\n");
        setbio(&firstpr ez,"George Washington", 58);
        printbio(&first prez);
        return 0;
        }

        void setbio(struct bio *p, const char *name, unsigned age)
        {
        strncpy(p->name,name,SZ_S TR);
        p->name[SZ_STR] = '\0'; /* truncation of large name */
        p->age = age;
        return;
        }

        void printbio(const struct bio *p)
        {
        if(*p->name != '\0')
        printf("name: %s\n age: %u\n",p->name,p->age);
        else puts("No bio");
        return;
        }

        --
        Al Bowers
        Tampa, Fl USA
        mailto: xabowers@myrapi dsys.com (remove the x to send email)
        Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!


        Comment

        • Darklight

          #5
          Re: is this correct

          Darklight wrote:

          Thanks for that

          Comment

          • Jim

            #6
            Re: is this correct

            On Wed, 30 Jun 2004 12:18:51 GMT, rlb@hoekstra-uitgeverij.nl (Richard
            Bos) wrote:
            [color=blue]
            >Darklight <nglennglen@net scape.net> wrote:[/color]
            [color=blue][color=green]
            >> 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]

            Yes, but only if it was
            struct entry list[1000];

            Jim

            Comment

            • Barry Schwarz

              #7
              Re: is this correct

              On Wed, 30 Jun 2004 12:18:51 GMT, rlb@hoekstra-uitgeverij.nl (Richard
              Bos) wrote:
              [color=blue]
              >Darklight <nglennglen@net scape.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).numbe r = 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(struc t data x); /* declare function prototype*/
              >> /* addditional statements go here */
              >> print_rec(rec); /* call function */[/color]
              >
              >Yes.
              >
              >Richard[/color]



              <<Remove the del for email>>

              Comment

              • Richard Bos

                #8
                Re: is this correct

                Barry Schwarz <schwarzb@deloz .net> wrote:

                [ Jeez, man, learn to snip! ]
                [color=blue]
                > On Wed, 30 Jun 2004 12:18:51 GMT, rlb@hoekstra-uitgeverij.nl (Richard
                > Bos) wrote:
                >[color=green]
                > >Darklight <nglennglen@net scape.net> wrote:
                > >[color=darkred]
                > >> 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]

                Correct; however, IMO if you want to gain a real understanding of
                pointers, arrays, and those kinds of struct members, you do need to
                understand that an array is not a pointer, even though it often behaves
                as one.

                Richard

                Comment

                Working...