|
|
|
struct StructName
{
dataType1 member1Name ;
dataType2 member2Name ;
...
};
|
Example:
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
|
Note:
|
Important note:
|
(The structure must have been define beforehand !!!)
struct StructName varName1 [, varName2, ...] ;
|
/* ---------------------------------------------
Structure definition
This MUST preceed any variable definition of this data type !!!
--------------------------------------------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
struct BankAccount a; // Defines a global structure variables
int main(int argc, char *argv[])
{
struct BankAccount b; // Defines a local structure variables
....
}
|
Result of a structure variable definition:
|
|
Consequently, a good C programmer must understand how a compiler works
(That's why I have injected information on how the C compiler works in the lecture material)
Example:
|
I will do the demo in class
How to run the program:
|
|
structVarName.componentName
|
Example:
#include <stdio.h>
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
struct BankAccount a; // Global variable
int main(int argc, char *argv[])
{
struct BankAccount b; // Local variable
a.accNum = 123; // Access member fields
a.balance = 1000.0;
b.accNum = 444;
b.balance = 3000.0;
printf("a.accNum = %d a.balance = %f\n", a.accNum, a.balance);
printf("b.accNum = %d b.balance = %f\n", b.accNum, b.balance);
}
|
How to run the program:
|
|
| struct_copy.java | struct-copy.c |
|---|---|
class BankAccount
{
int accNum;
double balance;
};
public class struct_copy
{
public static BankAccount a;
public static void main(String[] argv)
{
BankAccount b;
a = new BankAccount( ); // Create the objects
b = new BankAccount( );
a.accNum = 123;
a.balance = 1000.0;
b.accNum = 444;
b.balance = 3000.0;
System.out.printf("a = (%d, %f)\n", a.accNum, a.balance);
System.out.printf("b = (%d, %f)\n\n", b.accNum, b.balance);
/* ========================
Copy object reference
======================== */
b = a;
System.out.printf("a = (%d, %f)\n", a.accNum, a.balance);
System.out.printf("b = (%d, %f)\n\n", b.accNum, b.balance);
/* ============================================
Proof that a and b are the SAME object
============================================ */
a.balance = 999999.0;
System.out.printf("a = (%d, %f)\n", a.accNum, a.balance);
System.out.printf("b = (%d, %f)\n\n", b.accNum, b.balance);
}
}
|
#include <stdio.h>
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
struct BankAccount a;
int main(int argc, char *argv[])
{
struct BankAccount b;
a.accNum = 123;
a.balance = 1000.0;
b.accNum = 444;
b.balance = 3000.0;
printf("a = (%d, %f)\n", a.accNum, a.balance);
printf("b = (%d, %f)\n\n", b.accNum, b.balance);
/* ========================
Copy struct
======================== */
b = a;
printf("a = (%d, %f)\n", a.accNum, a.balance);
printf("b = (%d, %f)\n\n", b.accNum, b.balance);
/* ============================================
Proof that a and b are different objects
============================================ */
a.balance = 999999.0;
printf("a = (%d, %f)\n", a.accNum, a.balance);
printf("b = (%d, %f)\n\n", b.accNum, b.balance);
}
|
The output of the Java program struct_copy.java is:
a = (123, 1000.000000) b = (444, 3000.000000) a = (123, 1000.000000) b = (123, 1000.000000) After we update a.balance = 999999.000000: a = (123, 999999.000000) b = (123, 999999.000000) // b.balance is also changed !! |
This shows that a and b points to the same object
The output of the C program struct-copy.c is:
a = (123, 1000.000000) b = (444, 3000.000000) a = (123, 1000.000000) b = (123, 1000.000000) After we update a.balance = 999999.000000: a = (123, 999999.000000) b = (123, 1000.000000) // b.balance is unchanged !! |
This shows that a and b are different objects