How do we declare a struct in other languages? How do we declare them in c?

 

Before entering into matter, we should begin by defining what a  STRUCT is.  A STRUCT is basically a new data type which can be manipulated in the same matter as any of the other basic data types (int, float, char, etc.) has the same properties as an array meaning that:

 

  • It has a fixed number or elements
  • Elements must be contiguous

 

WITH THE EXEPTION OF ONE OF THE PROPERTIES OF AN ARRAY

 

  • All elements must be of the SAME data type

 

As a matter of fact, a STRUCT could be made of two or more data types.  One common example, and perhaps the easiest to understand and explain, are DATABASES.

 

NAME

SSN

DOB

ADDRESS

OCCUPATION

AGE

Alfred Molina

987654321

6/25/1985

259 La Colina

Student

18

 

In this example, the following data types were used.

 

FIELD

DATA TYPE

Name

char*

SSN

char*

DOB

char*

Adress

char*

Occupation

char*

Age

Int

 

The preceding information raises one question

 

 

?How many and what data types may be used in a STRUCT

          There is no best way to combine the data types, they can be combined to suit the needs of the programmer.  As far as how many data types      can be used the topic is still highly debatable, consider the following      table as the range of possible combinations.

 

Data Item

Data Type (variable)

Example

Comments

Name

char name[???];
char lastname[??],
firstname[??];

“Smith, Joe”
“Smith”
“Joe”

How long is a name?
Should we break up name?
How long should each be?

SSN

char ssn[11];
char ssn[9];
long ssn;
unsigned long ssn;

“123-45-6789”
“123456789”
123456789
123456789

If we include hyphens
Without hyphens
Max Val: 2,147,483,647
Max Val: 4,294,967,295

DOB

char DOB[6]

4/6/1982

This is highly subjective, it depends on the preferred date format.

Address

char street[???];
char apt[???],
street[???];
char street1[???],
street2[???];

123 Main St.”
Apt. 5A
123 Main St.”
Mesa Village
123 Main St.”

How Many Characters?
Should we have this?
How Many Characters?
Should there be two lines?
How Many Characters?

Occupation

char occupation[???];

"Student"

How long is occupation?

Age

unsigned long

18

This one is pretty easy.

 

 

 

How do we declare a struct in other languages?

 

To declare a STRUCT in another language one must obey the language’s instructions. For instance:

 

COBOL 

 

            01 Customer Info

                        02 Name                      PIC X(40).

                        02 SSN                        PIC 9(9).

                        02 DOB                       PIC 9(6).

                        02 Address                  PIC X(35).

                        02 Occupation PIC X(30).

                        02 Age                         PIC 9(3).

                       

For instance, in COBOL the X or 9 in after PIC identifies the code on the specific field.  For instance, 9 is numeric and X is Alpha-Numeric

 

 

 

PASCAL

           

            Type Customer Info = record

                                    Name : array [1..40] char

                                    SSN : interger;

                                    DOB : integer;

                                    Adress :array[1..35] of char;

                                    Occupation : array [1..30];

                                    Age : integer;

                        end;

 

In PASCAL, the procedure is similar to c.  The type of data to be used is declared after the title of the field.

 

These are just a couple of examples; the code will vary in accordance with the programming scheme of the language used.

 

 

How do we declare a STRUCT in c?

 

When using c, we basically use a compilation of previously used data types and generate a new and distinct data type. The steps to declare a STRUCT in c are as follows

 

1        Continuing with the database example, we begin by determining what fields we wish to have in our record.

2        We then proceed to determine the data type for each to be used for each of the fields.

3        We then determine the amount of bytes to be assigned to each of the fields.

1

 
 


FIELD

Name

SSN

DOB

Adress

Occupation

Age

Data Type

char

char

char

char

char

3

 

2

 
int

Bytes

31

10

5

41

31

2

 

4   We conclude bye declaring the RECORD in c with accordance with the    information previously selected.

 

struct Customer Info

{  char  Name [31];

    char  SSN [10];

    char  DOB [5];

    char  Address [41];

    int     age [2];  };

 

 

int main()

{  struct Custmer Info active =

       {"Favre, Brett",12345,"CIS",3.27,67};

   struct student alumni =

       {"White, Reggie", 23456, "Pain", 1.34, 89};

 

   printf("Name:  %13s %14s\n",active.student_name,

                               alumni.student_name);

   printf("ID:    %13d %14d\n",active.student_id,

                               alumni.student_id);

   printf("Major: %13s %14s\n",active.major,

                               alumni.major);

   printf("gpa:   %13.3f %14.3f\n",active.gpa,

                                   alumni.gpa);

   printf("Hours: %13d %14d\n",active.total_hrs,

                               alumni.total_hrs);

   return 0;}

 

 

*?Why is there an extra byte allocated for Name, SSN, DOB, Address, Occupation, and Age if they should consist of 30, 10, 5, 40, and 30 correspondingly.

          The reason for this, is because we are storing these fields as the data type        character array, which means that we need to contemplate one extra byte for the null character. If we do not do this, we would have to keep track of the length of each of the strings.

 

?How many total Bytes are needed for a STRUCT

 

a)     128 non-contiguous bytes.

b)     128 contiguous bytes.

c)     64 contiguous bytes.

d)     less than 64 bytes.

 

ANSWER: B

 

?What is the primary difference between a STRUCT and an array

a)     There is no difference they are the same.

b)     A Struct can only have one data type.

c)     A Struct can have several data types.

d)     A array can have does not need contiguous space.

 

          ANSWER: C

 

OTHER GOOD REFERENCES

          How stuff works

          Ohio State University

          Intro to STRUCT