Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
This page looks at some variations and ways for parameter passing involving record structures.
You should be familiar with variations and ways for parameter passing involving variables and arrays at C: routine variations for variables .
2. Problem
The problem is to initialize two fields of different record structures and then call a function or procedure to add those fields in some way.
The following examples are some of the ways in which two integers can be added. In doing so, various methods are used involving record structures.
3. Structure
The structure will be called data and consists of one integer field value.
The use of one field may be useful if it is expected that this record structure will sometime in the future need additional fields. The code is ready for that without change.
4. Local variables in main
Three local variables are needed in main.
5. Assignment
To keep the code simple, the values are directly assigned.
6. In-line process and output
Here is the sum calculated by in-line code.
7. Call by value
Call by value passes a copy of the values to the function. Here is the function.
Here is the corresponding call in main.
8. Call by reference
Call by reference passes a reference (pointer) of the values to the function. Here is the function.
The usual way to do call by reference using a pointer is to use "*a1.value". But this causes an error since field value is not being passed by reference. One must write "(*a1).value" instead.
Here is the corresponding call in main.
9. Shorthand notation
As a shortcut for the notation "(*a1).", one can (and should) use "a1 -> ". And since parameters a1 and b2 are not going to be changed, the const keywoard can be added.
The call in main remains the same.
10. Function to/from procedure
It is often necessary to change a function f (as an expression) to or from a procedure p (as a statement). Here is the general example form (for integers).
The function f or procedure p must be written accordingly. Here is the general example form (for integers), written on one line to show the differences.
Here is the C code.
11. Function to procedure
Returning to the original problem, using call by value, the function add2 can be converted to a procedure as follows.
The call needs to be changed appropriately to the following.
12. Shorthand notation
The same changes can be made to this procedure. Note that const can be used with both value and reference parameters.
Here is the call.
By convention (and in most cases) all formal parameters into the function are at the beginning of the list and all formal parameters changed (by reference) are at the end of the parameter list.
13. Entire program
Here is the C code.
Here is the output of the C code.
14. Summary of record access
When accessing a field of a record structure, the dot "." is usually used to access the desired field.
But, in certain cases when the record structure variable is a formal parameter and that formal parameter is called by reference, another notation is used.
When field y of record structure is accessed of formal parameter x representing a record structure, the usual way is to use the asterisk "*" for the "pointer" formal parameter variable as follows.
(*x).y
Note: The form *x.y is ambiguous so the parentheses is used.
The shorthand notation for the above is as follows using the arrow "->" notation (dash followed by greater than character).
x -> y
Bottom line:
Use "." for variables (accessor).
Use "->" for pointers (dereferencer and accessor).
Note for record structure variables:
Local variables should use ".".
Call by value formal parameter variables should use ".".
Call by reference formal parameter variables should use "->".