Pointer to user-defined (struct) variables

• Review: (1) reference (pointer) variables and (2) user-defined types (struct)

• Recall:

• Reference data type:

 ``` dataType* varName ; ```

define a variable varName that can hold an address of a variable of type dataType (See: click here )

• Recall:

• Structure (record) definition:

 ``` struct StructName { dataType1 varName1 ; dataType2 varName2 ; ... } ```

define a data type with the name struct StructName that has the fields (components) varName1 of the data type dataType1, varName2 of the data type dataType2, ... (See: click here )

• Pointer (reference) to struct variables

• Address of a struct typed variable:

• Consider these 2 struct BankAccount variables:

Observe that:

 The variable a is located at address 5000 in memory. The variable b is located at address 5012

• Reference (pointer) variable to a struct BankAccount variable:

 Reference variable to a struct BankAccount variable = a variable that can contain an address of a struct BankAccount variable

• Syntax to define a reference (pointer) variable to a struct datatype:

 ``` struct StructName *varName ; ```

Meaning:

 defines a variable named varName that can store an address of a variable of the type struct StructName.

• How to use reference (pointer) veriables to struct typed variables:

 I will explain how to use reference (pointer) veriables to struct typed variables with an analogy - comparing its usage with how it was used in simple (int, double, etc) variables

• How to use the reference operator "&" and dereference operator "*" on struct variables

• Recall the reference operator:

• Reference operator &

 ``` int a; &a is the address of the int variable a Analogously: (when applied to a struct) struct BankAccount b; &b is the address of the struct BankAccount b ```

• Recall the dereference operator:

• Dereference operator *:

 ``` int a; int *p = &a; *p is an alias for the variable a Analogously: (when applied to a struct) struct BankAccount b; struct BankAccount *p = &b; *p is an alias for the variable b ```

• How to use reference variables to struct typed variables:

• Make a struct reference variable point (reference) to a struct typed variable:

 ``` struct StructName *p ; // reference variable to a struct typed variable struct StructName a ; // A struct typed variable ... p = &a; // Make p point to the struct typed variable a ```

Illustrated (using the BankAccount structure):

• Accessing component variables inside a struct using a reference typed variable:

 ``` struct StructName *p ; // reference variable to a struct typed variable struct StructName a ; // A struct typed variable ... p = &a; // Make p point to the struct typed variable a // *p is now an alias for a !!! a.accNum access the accNum component in a (*p).accNum access the accNum component through p ```

Illustrated (using the BankAccount structure)

• Syntax note:

• Fact:

 The   .   (member selection) operator has a higher priority than the * (dereference) operator !! See: click here

• Therefore:

 ``` *p.accNum means: *(p.accNum) Which is in error, because * is applied to "p.accNum" (The variable accNum is NOT a pointer variable !!!) * should be applied to p !!! ```

That's why we must use this syntax:

 ``` (*p).accNum ```

so that the * (dereference) operator is applied to p !

• Example program:

 ``` 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; struct BankAccount *p; // Define a ref. var. to a struct p = &a; // Make p point to a printf("After p = &a\n"); printf("(*p).accNum = %d (*p).balance = %f\n", (*p).accNum, // = (*p) is a, so (*p).accNum is a.accNum (*p).balance); // = (*p) is a, so (*p).balance is a.balance p = &b; // Make p point to b printf("After p = &b\n"); printf("(*p).accNum = %d (*p).balance = %f\n", (*p).accNum, // = (*p) is b, so (*p).accNum is b.accNum (*p).balance); // = (*p) is b, so (*p).balance is b.balance } ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   gcc struct-ref1.c To run:          ./a.out