Structures:
user-defined data types in C
- Structure:
- A
structure in
a C program is
a group of
related variables
|
Example:
- A
BankAccount
structure
may consists of
the related variables
ID and
balance
|
|
Defining a
C struct
- Syntax to
define a
struct (user defined)
data type in
C:
struct structureName
{
variable definitions;
} [varname1, varName2] ; // Optional elements
|
- Example:
struct BankAccount {
int ID;
float balance;
} ;
|
-
Important note:
- A
struct data type definition
is in the global scope
-
struct data type definitions
must be placed
outside
every function !
|
|
Comparing C's struct with
Java's class construct
- The
struct
definition
in C is
similar to
a
class definition
in Java:
C's struct
|
Java's class
|
struct BankAccount
{
int ID;
float balance;
}
|
public class BankAccount
{
public int ID;
public float balance;
public void deposit(double x)
{
balance += x;
}
}
|
- Differences:
-
No access protection
of member fields in
a C's struct
- A struct
does
not
contain any
(member) function definitions
|
|
Defining (user-defined) struct typed
variables
- You can define
struct typed
variable(s)
after defining the
struct
data type
Rule:
- The struct (data type) definition
must
preceed
any variable definition of
that structure data type !!!
|
- Syntax to
define a
struct typed
variable in
C:
struct structName varName ;
|
Example:
struct BankAccount // (1) Must define struct first
{
int ID;
float balance;
} ;
struct BankAccount john, mary; // (2) Then define variable(s)
|
|
Using a struct typed variable
Using a
struct typed variable
Example program:
defining and
using
struct typed variables
#include <stdio.h>
// (1) You must first define the struct definition
struct BankAccount {
int ID;
float balance;
};
// (2) Afterwards, you can define struct typed variables
struct BankAccount mary; // Global struct variable
int main( int argc, char *argv[] )
{
struct BankAccount john; // Local struct variable
mary.balance = 900;
john.balance = 500;
printf("%f\n\n", mary.balance + john.balance);
}
|
DEMO:
demo/C/set2/struct1.c
Fundamental difference
between
struct variables
in C and
class variables
in Java
- There are
2 (two)
fundamental
differences between
C's
struct and
Java's
objects:
- How
to
find
an
object/struct:
- Java uses
a reference
variable
to
record the
location of
an object
- C uses
a
symbolic constant
to
record the
location of
an struct
|
- The
meaning of the
(variable) name of an
object/struct variable:
Java C
--------------------------- --------------------------
myClass x; struct myStruct x;
x = the reference to x = the entire struct
an object (="object")
|
|
|
Fundamental difference
between
struct variables
in C and
class variables
in Java
- Class variables
in Java are
reference variables to
objects
(that are created by
new):
BankAccount a, b; // Define 2 reference variables
|
- The
BankAccount objects
are created using:
a = new BankAccount( );
b = new BankAccount( );
|
Result:
|
Fundamental difference
between
struct variables
in C and
class variables
in Java
Fundamental difference
between
struct variables
in C and
class variables
in Java
Example
Java program that
shows
b = a
makes b an
alias of
a
public class struct_copy
{
public static BankAccount a;
public static void main(String[] argv)
{
BankAccount b;
a = new BankAccount( ); // Create an object
b = new BankAccount( ); // Create another object
a.ID = 123;
a.balance = 1000.0;
b.ID = 444;
b.balance = 3000.0;
System.out.printf("a = (%d, %f)\n", a.ID, a.balance); // a and b are
System.out.printf("b = (%d, %f)\n\n", b.ID, b.balance); // diff objs
b = a; // b is now alias of a
a.balance = 999999.0;
System.out.printf("a = (%d, %f)\n", a.ID, a.balance); // 123, 999999.0
System.out.printf("b = (%d, %f)\n\n", b.ID, b.balance); // 123, 999999.0
}
|
DEMO:
demo/C/set2/struct_copy.java
Fundamental difference
between
struct variables
in C and
class variables
in Java
Fundamental difference
between
struct variables
in C and
class variables
in Java
Fundamental difference
between
struct variables
in C and
class variables
in Java
Example
C program that
shows
b = a
will copies
the values in
a over to
b:
struct BankAccount
{
int ID;
double balance;
};
struct BankAccount a;
int main(int argc, char *argv[])
{
struct BankAccount b;
a.ID = 123;
a.balance = 1000.0;
b.ID = 444;
b.balance = 3000.0;
printf("a = (%d, %f)\n", a.ID, a.balance); // a and b are
printf("b = (%d, %f)\n\n", b.ID, b.balance); // diff objs
b = a; // Copies content of a to b
a.balance = 999999.0;
printf("a = (%d, %f)\n", a.ID, a.balance); // 123, 999999.0
printf("b = (%d, %f)\n\n", b.ID, b.balance); // 123, 1000.0
}
|
DEMO:
demo/C/set2/struct-copy.c
A quick way to
define global
struct typed variables
- You may include
some
variable names when you
define a
struct:
struct BankAccount
{
int ID;
float balance;
} john; // Also defines a (global) "struct" variable
// along with the "struct" data type
|
- Note:
- You cannot define
a
local
struct variable
in this manner
(because the
struct definition
must be
global)
- You can only define
global
struct variables
this way.
|
- This feature is
handy if
your program
only uses
a few
global
struct variables
of the specific type....
|
❮
❯