|
|
|
struct StructName
{
dataType1 member1Name ;
dataType2 member2Name ;
...
};
|
Note:
|
|
(The structure definition acts like a blue print to construct variable of that structure).
|
struct StructName varName1, varName2, ... ;
|
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
int main(int argc, char *argv[])
{
struct BankAccount a, b; // Defines 2 structure variables
....
}
|
|
|
structVarName.componentName
|
Example:
#include <stdio.h>
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
int main(int argc, char *argv[])
{
struct BankAccount a, b;
a.accNum = 123;
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:
|
|
|
extern struct StructName varName ; |
Important caveat:
|
| main program file (defines a) | help program file (declares a) |
|---|---|
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define a struct variable
------------------------------------ */
struct BankAccount a;
int main(int argc, char *argv[])
{
a.accNum = 123;
a.balance = 1000.0;
print(); // Invoke print() defined
// inside a different file
}
|
/* --------------------------
Define the structure FIRST
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ----------------------
Now you can declare a
---------------------- */
extern struct BankAccount a;
// Declare structure variable a
// that is defined ELSEWHERE
void print()
{
printf("a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
}
|
How to run the program:
|
| main program file (defines a) | help program file (declares a) |
|---|---|
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define a struct variable
------------------------------------ */
struct BankAccount a;
int main(int argc, char *argv[])
{
a.accNum = 123;
a.balance = 1000.0;
print(); // Invoke print() defined
// inside a different file
}
|
/* --------------------------
REPEAT the definition !!!
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ----------------------
Now you can declare a
---------------------- */
extern struct BankAccount a;
// Declare structure variable a
// that is defined ELSEWHERE
void print()
{
printf("a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
}
|
This can result in errors caused by typos...
|
returnType functionName ( .... , struct StructName param , .... )
{
// (Function body)
}
|
Always remember that:
|
| main program file (defines a) | help program file (declares a) |
|---|---|
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define variables
of that structure...
------------------------------------ */
int main(int argc, char *argv[])
{
struct BankAccount a;
a.accNum = 123;
a.balance = 1000.0;
print( a ); // Pass a struct
// variable as parameter
}
|
/* --------------------------
Define the structure FIRST
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* -------------------------------
Now you can define a paramter
variable of that structure.....
------------------------------- */
void print( struct BankAccount x )
{
printf("x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
}
|
How to run the program:
|
|
| main program file (defines a) | help program file (declares a) |
|---|---|
/* ---------------------------
Define the structure FIRST
---------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* ------------------------------------
Now you can define variables
of that structure...
------------------------------------ */
int main(int argc, char *argv[])
{
struct BankAccount a;
extern void update( struct BankAccount x );
// Declare update()
a.accNum = 123;
a.balance = 1000.0;
printf("Before calling update:
a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
update (a);
// Demonstrate that struct is
// passed-by-value
printf("AFTER calling update:
a.accNum = %d a.balance = %f\n",
a.accNum, a.balance);
}
|
/* --------------------------
Define the structure FIRST
-------------------------- */
struct BankAccount
{
int accNum;
double balance;
};
/* -------------------------------
Now you can define a paramter
variable of that structure.....
------------------------------- */
void update( struct BankAccount x )
{
printf("Before update:
x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
x.accNum = 1001; // Update parameter
x.balance = 999999; // variable
printf("AFTER update:
x.accNum = %d x.balance = %f\n",
x.accNum, x.balance);
}
|
Result:
Before calling update: a.accNum = 123 a.balance = 1000.000000 Before update: x.accNum = 123 x.balance = 1000.000000 AFTER update: x.accNum = 1001 x.balance = 999999.000000 AFTER calling update: a.accNum = 123 a.balance = 1000.000000 UNCHANGED !!! |
How to run the program:
|
|
| Main function passes array inside struct | Functions updates 2 elemenst in array |
|---|---|
/* ------------------------
Structure definition
------------------------ */
struct Array
{
int a[10]; // Array in struct
};
extern void update( struct Array x );
int main(int argc, char *argv[])
{
struct Array a;
int i;
for ( i = 0; i < 10; i++ )
a.a[i] = i;
printf("Before calling update: ");
for ( i = 0; i < 10; i++ )
printf("%d ", a.a[i]);
putchar('\n');
update (a);
// Demonstrate that array inside
// a struct is passed-by-value
printf("AFTER calling update: ");
for ( i = 0; i < 10; i++ )
printf("%d ", a.a[i]);
putchar('\n');
}
|
/* ------------------------
Structure definition
------------------------ */
struct Array
{
int a[10];
};
void update( struct Array x )
{
int i;
printf("Before updating array in x: ");
for ( i = 0; i < 10; i++ )
printf("%d ", x.a[i]);
putchar('\n');
x.a[0] = 4444;
x.a[9] = 4444;
printf("AFTER updating array in x: ");
for ( i = 0; i < 10; i++ )
printf("%d ", x.a[i]);
putchar('\n');
}
|
Result:
Before calling update: 0 1 2 3 4 5 6 7 8 9 Before updating array in x: 0 1 2 3 4 5 6 7 8 9 AFTER updating array in x: 4444 1 2 3 4 5 6 7 8 4444 AFTER calling update: 0 1 2 3 4 5 6 7 8 9 (UNCHANGED) |
How to run the program:
|
struct BankAccount
{
int accNum;
double balance;
};
struct BankAccount f( )
{
struct BankAccount p; // Define a BankAccount variable
p.accNum = 4;
p.balance = 4444.0;
return p; // Return the BankAccount variable
}
|
struct BankAccount a; a = f ( ); |
How to run the program:
|
|
Example:
/* ------------------------
Structure definition
------------------------ */
struct BankAccount
{
int accNum;
double balance;
};
int main(int argc, char *argv[])
{
printf("sizeof(struct BankAccount) = %d\n",
sizeof(struct BankAccount) );
}
|
Output:
sizeof(struct BankAccount) = 16
|
How to run the program:
|
|
Answer:
|
struct s1
{
char a;
int d;
};
struct s2
{
char a;
char b;
int d;
};
struct s3
{
char a;
char b;
char c;
int d;
};
int main(int argc, char *argv[])
{
printf("sizeof(struct s1) = %d\n", sizeof(struct s1) );
printf("sizeof(struct s2) = %d\n", sizeof(struct s2) );
printf("sizeof(struct s3) = %d\n", sizeof(struct s3) );
}
|
Output:
sizeof(struct s1) = 8 sizeof(struct s2) = 8 sizeof(struct s3) = 8 |
Explanation:
|
How to run the program:
|
|