summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/pointer_func.c
diff options
context:
space:
mode:
Diffstat (limited to 'reference/C/EXAMPLES/pointer_func.c')
-rw-r--r--reference/C/EXAMPLES/pointer_func.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/reference/C/EXAMPLES/pointer_func.c b/reference/C/EXAMPLES/pointer_func.c
new file mode 100644
index 0000000..37e1bfd
--- /dev/null
+++ b/reference/C/EXAMPLES/pointer_func.c
@@ -0,0 +1,35 @@
+/************************************************************************
+ *
+ * Purpose: Program to demonstrate passing a pointer to a scalar to
+ * a function.
+ * Author: M J Leslie.
+ * Date: 11-Apr-94
+ *
+ ************************************************************************/
+
+void add(int*); /* Function declaration */
+
+main()
+{
+ int i=4; /* variable declaration */
+ int* ptr; /* int pointer */
+
+ ptr = &i; /* 'ptr' now contains the
+ address of 'i' */
+
+ printf("i starts out life as %d.\n", i);
+ printf(" *ptr is %d.\n", *ptr);
+
+ add(ptr); /* Function call */
+
+ printf(" i is now %d.\n", i);
+}
+
+/************************************************************************/
+
+void add(int *ptr) /* Function definition */
+{
+ ++*ptr; /* Add 1 to the value
+ * pointed too by 'ptr' */
+ return;
+}