CONTENTS:
• I. C CALL BY REFERENCE VERSUS CALL BY VALUE INTRODUCTION
• II. MULTIPLE INDIRECTION
• III. C CALL BY REFERENCE VERSUS CALL BY VALUE EXAMPLE PROGRAM SOURCE CODE
• IV. C CALL BY REFERENCE VERSUS CALL BY VALUE REFERENCES
YOU WILL LEARN:
1. C call by reference versus call by value.
2. Multiple indirection.
3. C pointers to pointers.
4. C arrays are passed by reference.
I. INTRODUCTION
Welcome to the “C Call by Reference Versus Call by Value Tutorial.”
This tutorial will demonstrate the use of pointers to pointers.
II. MULTIPLE INDIRECTION
First a quick review:
When C passes arguments to functions it passes them by value; except for arrays, which are passed by reference. When you are passing a variable to a function you are passing a copy of the variable. When you are passing an array to a function you are passing a pointer to the first element of the array.
All functions have the following form:
type function name(arguments passed to the function)
variable declaration of the arguments
{
local variable declarations
statements
}
A. Call by value
The function works with a copy of the variable.
Changes made to the variable in the function will not effect the value of the variable in the calling function.
B. Call by reference
The address of the variable is passed to the function.
Changes made to the variable in the function will effect the value of the variable in the calling function.
A function can only have one return value. However, to modify the value of variables in a function, pass the addresses of those variables as function parameters. The reference which locates a variable address in memory can be obtained by preceding the identifier of the variable in the calling function arguments with an ampersand sign (&),"address of" reference operator.
A pointer is a special type of variable that holds the reference, the address, of the value of another variable. Pointers "point to" the variable whose reference they store. A variable points to a value. The value stored in the variable it points to, can be directly accessed by preceding the pointer's identifier with an asterisk (*), "value pointed by", dereference operator.
As shown in the first tutorial of this series, the following two dimensional character array:
char GRACE[4][11]= { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy";
Can also be coded as:
char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" };
Also, to point to an element of an array of elements, you need a pointer to the element type. To point to an element of an array of type char *, you need a pointer to type char *. In other words, you need a type char **, a pointer to a pointer to a char, as follows:
char** p = GRACE;
Lets distinguish between the pointer, and what it points to; and what the pointer that it points to, points to.
The pointer *p, points to *GRACE[0]; and the pointer *GRACE[0], points to "Genesis”. Therefore, **p is a pointer that points to a pointer that points to “Genesis”.
As shown below, the prefix operators ++ which appear before the pointer p,; i.e., ++p, are used to point to the next element in the array, resulting in the printing of each character string literal in the array, in array element sequence, to the screen. The compiler uses the size of the data type, which it knows from the pointer's type. This works because all pointers of a given type, have the same memory address size.
{
printf(" %s", *p );
++p;
}
After the first ++p in the “for loop iteration” shown above:
The pointer *p, points to *GRACE[1]; and the pointer *GRACE[1], points to "Exodus". Therefore, **p is now a pointer that points to a pointer that points to "Exodus".
After the second ++p “for loop iteration”:
The pointer *p, points to *GRACE[2]; and the pointer *GRACE[2], points to "Leviticus". Therefore, **p is now a pointer that points to a pointer that points to "Leviticus".
After the third ++p “for loop iteration”:
The pointer *p, points to *GRACE[3]; and the pointer *GRACE[3], points to "Numbers". Therefore, **p is now a pointer that points to a pointer that points to "Numbers".
After the fourth ++p “for loop iteration”:
The pointer *p, points to *GRACE[4]; and the pointer *GRACE[4], points to "Deuteronomy". Therefore, **p is now a pointer that points to a pointer that points to "Deuteronomy".
In general, the & operator adds asterisks, increasing pointer level. And, the *, ->, and []operators remove asterisks, decreasing pointer level.
III. C CALL BY REFERENCE VERSUS CALL BY VALUE EXAMPLE PROGRAM SOURCE CODE
/***********************************
C CALL BY REFERENCE VERSUS CALL BY VALUE
***********************************/
#include<stdio.h>
// standard I/O header file
#include<string.h>
// string handling
int main(int argc, char* argv[])
{
char* GRACE[] = { "Genesis", "Exodus", "Leviticus", "Numbers", "Deuteronomy" };
char** p = GRACE;
printf("\n\n C CALL BY REFERENCE VERSUS CALL BY VALUE\n\n\n");
printf("\n\n THE FIRST FIVE BOOKS OF THE BIBLE ARE:\n\n\n");
for( unsigned n = 0; n < 5; ++n )
{
printf(" %s", *p );
++p;
}
printf("\n\n\n\n\n\n\n\n Please press any key to EXIT.");
char c;
c = getchar();
return 0;
}
IV. C CALL BY REFERENCE VERSUS CALL BY VALUE REFERENCES
The C Programming Language by Brian Kernighan and Dennis Ritchie (Englewood Cliffs, N.J.: Prentice-Hall, 1978).
C++: The Complete Reference, Fourth Edition by Herbert Schildt (Berkeley, California: McGraw-Hill/Osborne, 2003).
1. C call by reference versus call by value.
2. Multiple indirection.
3. C pointers to pointers.
4. C arrays are passed by reference.
Elcric Otto Circle
-->
-->
How to Link to My Home Page
It will appear on your website as:"Link to ELCRIC OTTO CIRCLE's Home Page"
No comments:
Post a Comment