CS 108 Computing Fundamentals
Notes for Monday, February 27, 2017
"Output" and/or "Input/Output" (aka pointer)
parameters (passed by address/reference)
void parameter_demo (int p_john , int * p_sally )
{
Data type of the variable to which the
name
“pointer” formal parameter points
* p_sally
Indirection operator
Notice that these
two addresses are
different
0xaf3a39d1
the address of the
0x2afe3da4
variable to which
* p_sally “points”
is changeable
address of * p_sally
in memory is not changeable
#include <stdio.h>
/* 5.c */
void parameter_demo (int , int * );
int main (void )
One-to-one agreement necessary
{
int john = 50;
int sally= 250;
printf("\n\n john's address: %p , john's value : %d ", &john , john);
printf("\n\n sally's address: %p , sally's value : %d ", &sally , sally);
parameter_demo (john, &sally);
printf("\n\n parameter_demo( ) PCF was called.");
printf("\n\n john's address: %p , john's value : %d ", &john , john);
printf("\n\n sally's address: %p , sally's value : %d ", &sally , sally);
printf("\n\n\n");
return 0;
}
void parameter_demo (int p_john , int * p_sally )
{
p_john = 77 ;
*p_sally = 77 ;
return ;
}
One-to-one agreement necessary
#include <stdio.h>
/* 6.c */
void parameter_demo (int , int * );
int main (void)
One-to-one agreement necessary
{
int john = 50;
int sally= 250;
printf("\n\n john's address:
%p , value : %d ", &john , john);
printf("\n\n sally's address:
%p , value : %d ", &sally , sally);
parameter_demo ( john, &sally);
printf("\n\n john's address:
%p , value : %d ", &john , john);
printf("\n\n sally's address:
%p , value : %d ", &sally , sally);
printf("\n\n\n");
return 0;
}
void parameter_demo ( int p_john , int * p_sally )
{
printf("\n\n p_john's address: %p , value : %d ", &p_john , p_john);
printf("\n\n p_sally's address: %p , value : %p , point's to: %d ", &p_sally , p_sally , *p_sally);
p_john = 77 ;
*p_sally = 77 ;
printf("\n\n Changes made: p_john = 77 and *p_sally = 77 ");
printf("\n\n p_john's address: %p , value : %d ", &p_john , p_john);
printf("\n\n p_sally's address: %p , value : %p , point's to: %d ", &p_sally , p_sally , *p_sally);
return ;
}
One-to-one agreement necessary
Pointer Variables
• A special kind of variable that is very
similar to "Output" and/or "Input/Output"
parameters
• A pointer variable has the same properties
as other variables: name, data type, variable
value, and address value
• A pointer "specialness": it is a variable that
holds the address value of another variable
as its variable (changeable) value
Pointer Variables
• A pointer variable has two addresses: its
own and that of another variable’s value
• A pointer variable’s data type refers to the
data type of the variable value to which it
“points” or refers.
Graphic Depiction of a
Pointer Variable
int *capacity_ptr = &arena_size;
arena_size
name
*capacity_ptr
data type
(of variable
value to which
it “points”)
9000
xaf3a39d1
0xaf3a39d1
0x2afe3da4
address
(the address of
the variable's
value to which
it “points”)
Graphic Depiction of a
Pointer Variable
int *capacity_ptr = &arena_size;
One-to-one agreement necessary
*capacity_ptr
0xaf3a39d1
0x2afe3da4
*: The Indirection Operator
• * (when used to declare/define/create a pointer variable
or when used to declare/define/create a parameter):
declare a memory object that is a pointer (informs the
compiler that we want to store addresses at this place in
memory)
• 7.c (in two slides) demo’s pointer variable’s declaration and
basic manipulation/use
*: The Dereferencing Operator
• * (when used as a function argument or when used in an
expression) means "take the address stored as the pointer’s
variable value and find what “true” value is stored at that
address" (dereferencing)
• * (when used on the left side of an assignment operator)
means "take the value on the right side of the assignment
operator and place it at the address held as the pointer’s
variable value" (dereferencing)
• 7.c (on next slide) demo’s pointer variable’s declaration and
basic manipulation/use
/* 7.c */
#include <stdio.h>
int main (void)
{
int stadium_size = 50000;
int arena_size = 9000;
int *capacity_ptr = &stadium_size;
Indirection
printf("\n\n Capacity_ptr points to the value: %d", *capacity_ptr);
capacity_ptr = &arena_size;
printf("\n\n Capacity_ptr points to the value: %d", *capacity_ptr);
printf("\n\n\n");
return 0;
}
Dereferencing
Pointer Variables Have Two Addresses
• A pointer variable holds the address of another
variable’s value as its variable value
• When a pointer is declared it is assigned an
address or location of its own (like any other
variable)
• 8.c (on the next slide) demo’s the “two
addresses” associated with each pointer
variable and the variable value to which the
pointer “points” or refers
#include <stdio.h>/* 8.c */
int main (void)
{
int stadium_size = 50000;
int *capacity_ptr = NULL;
int *sunyit_ptr = NULL;
capacity_ptr = &stadium_size;
sunyit_ptr = &stadium_size;
printf("\n stadium_size - variable value:
stadium_size , &stadium_size);
printf("\n\n capacity_ptr's address:
printf("\n\n capacity_ptr's variable value:
printf("\n\n capacity_ptr points to the value:
%p", &capacity_ptr);
%p", capacity_ptr);
%d", *capacity_ptr);
printf("\n\n sunyit_ptr's address:
printf("\n\n sunyit_ptr's variable value:
printf("\n\n sunyit_ptr points to the value:
%p", &sunyit_ptr);
%p", sunyit_ptr);
%d", *sunyit_ptr);
printf("\n\n\n");
return 0;
}
%d / address: %p",
Graphic Depiction of a
Pointer Variable
*capacity_ptr
*sunyit_ptr
0x7fffffffe91c
0x7fffffffe91c
0xbfbff768ffb2
0xbfbff764bbc1
stadium_size
50000
0x7fffffffe91c
Assigning a Variable Value to the Object to
Which a Pointer Variable is Pointing
int stadium_size = 50000; int *capacity_ptr = NULL;
int *sunyit_ptr = NULL; capacity_ptr = &stadium_size;
sunyit_ptr = &stadium_size;
*capacity_ptr = 7;
printf("\n\n sunyit_ptr points to the value: %d", *sunyit_ptr);
printf("\n\n capacity_ptr points to the value: %d", *capacity_ptr);
*sunyit_ptr = *sunyit_ptr + 1;
printf("\n\n sunyit_ptr points to the value: %d", *sunyit_ptr);
printf("\n\n capacity_ptr points to the value: %d", *capacity_ptr);
• 9.c (on the next slide) demo’s assignments through pointers
#include <stdio.h>/* 9.c */
int main (void)
{
int stadium_size = 50000; int *capacity_ptr = NULL;
int * sunyit_ptr = NULL; capacity_ptr = &stadium_size;
sunyit_ptr = &stadium_size;
printf("\n stadium_size - variable value:
%d / address value: %p",
stadium_size, &stadium_size);
*capacity_ptr = 7;
printf("\n\n sunyit_ptr points to the value: %d", *sunyit_ptr);
printf("\n\n capacity_ptr points to the value: %d", *capacity_ptr);
*sunyit_ptr = *sunyit_ptr + 1;
printf("\n\n sunyit_ptr points to the value: %d", *sunyit_ptr);
printf("\n\n capacity_ptr points to the value: %d", *capacity_ptr);
printf("\n\n\n stadium_size - variable value: %d / address value: %p",
stadium_size, &stadium_size);
printf("\n\n\n");
return 0;
}
Single Variables are Boring!!
Single Variables are Boring!!
• There are lots of times we’d like to work with a
bunch of "like things"
– we can refer to this "bunch of like things" as a
homogeneous group or collection
– In C the concept of being homogeneous or the
"homogeneity" of groups/collections means the
member of the group have the same data type
• Here’s an example of a collection of floats:
1.4
7.1
5.5
17.0
6.1
3.14
Single Variables are Boring!!
• Groups or collections of items
– Because they are the same type we need a way to distinguish
one from the other
– "order" to the rescue… intuitively we might use these labels
first item second item
1.4
fifth item
17.0
7.1
sixth item
6.1
third item
fourth item
3.14
5.5
Single Variables are Boring!!
• Our intuition is off by 1… in C an ordered list does not
begin with "first" item… it begins with zeroth item
zeroth item first item
1.4
fourth item
17.0
7.1
fifth item
6.1
second item
3.14
third item
5.5
Single Variables are Boring!!
• zeroth, first, second, etc… are too long
– We need a little shorthand… we can barrow from matrix notation
– While we’re at it, let’s name the collection (we’ll call this one A)
A0
A1
A2
1.4
7.1
3.14
A4
A5
17.0
6.1
A3
5.5
Single Variables are Boring!!
• This shorthand works, too… we prefer this method
A[0]
A[1]
A[2]
A[3]
1.4
7.1
3.14
5.5
A[4]
A[5]
17.0
6.1
An Array
• A "data structure" in which items of the same data
type are stored sequentially (in order):
– item 0, item 1, item 2, item 3...
• These homogenous data items are stored in
memory in sequential order (contiguously):
index[0]
item 0
index[1]
item 1
index[2] index[3]
item 2
item 3
Arrays
• For now, as we introduce the concept of an array,
think of an array as a list
• Array named "snow": [13.2, 7.3, 4.4, 65.3]
• Each position is accessed by a "subscript" or
"index value":
snow[0]
snow[1]
snow[2]
snow[3]
13.2
7.3
4.4
65.3
Arrays
• snow0 or snow[0] is 13.2
• snow1 or snow[1] is 7.3
• etc...
• Soon we will expand the notion of an array
beyond a simple list
– We can easily use the array data structure to construct
things like matrices and tables
Arrays
• Why do arrays in C start with 0?
– The subscript tells how far from the beginning
of the array it is to the element we want.
This is known as the offset.
Offset from what?
From the array’s address!!
Arrays
• An array shares traits with single variables:
– Name (also known as an identifier)
– Data type
– Address
An array has a "address" and each value (element) in the
array is found using the "offset" from this address
– Value... instead of a single value, an array has the
capability to hold multiple values
– One value per array element
Array Declaration
• To declare an array you need:
– Data type (what kind of values will the array
hold?)
– Name/Identifier
– Size (how many different values will the array
hold?)
int golf_score[18];
– As with other variables, the compiler/OS
handle assigning an array its address in memory
with enough space to hold the array's values
Array Declaration
• Programmers often use a constant to specify the
size of an array:
const int SIZE = 18;
int golf_score[SIZ];
or
#define SIZE 18
…
int golf_score[SIZE];
Array Declaration
Array Initialization
Arrays and Input/Output
• Loops are commonly used to facilitate efficient
input/output and traversing for arrays
...
const int SIZE = 18;
int golf_score[SIZE];
...
for ( i = 0; i < SIZE ; i = i + 1 )
{
printf("\n\n Enter a golf score: ");
scanf("%d", &golf_score[i]);
}
Arrays and Input/Output
• Print all the elements of an array, one element per line
for ( i = 0 ; i < size ; i++ )
{
printf("%d \n ", golf_score[i]);
}
Arrays and Input/Output
• Print all the elements of an array with their element
subscripts (index)… in this case print each hole number
and its corresponding score and put each hole and score
on a different line... we often use i to indicate "index"
for ( i = 0 ; i < size ; i++ )
{
printf("Hole %d score was %d \n ", i + 1, golf_score[i]);
}
OR
for ( i = 1; i <= size ; i++ )
{
printf("Hole %d score was %d\n", i , golf_score[ i - 1]);
}
Array Indices
• Highest and lowest indices form the bounds of the array
• In C, the lower bound is always 0
• In C, the upper bound is always 1 less than the size of the
array (remember: the array begins with 0 not 1)
• C cannot check array bounds… it must be done manually
Array "Bounds" Checking
const int size = 18;
int golf_score[size];
int hole = 0;
printf("For which hole do want the score? ");
scanf ("%d", &hole);
if ( (hole >= 1) && (hole <= size))
{
printf ("\n\nOn hole number %d you carded: %d\n",
hole, golf_score[hole-1]);
}
else
#include <stdio.h> //My file 10.c
#define size 18
int main ( )
{
int golf_score[size] = {2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36};
int hole = 0;
printf("\n\nFor which hole do want the score? ");
scanf ("%d", &hole);
if (( hole >= 1) && (hole <= size))
{
printf ("\n\nOn hole number %d you carded: %d\n", hole,
golf_score[hole-1]);
}
else
printf("\n\nSorry, but your entry of hole %d is not appropriate.\n", hole);
return 0;
}
Array Practice
Let’s write a program that:
- prompts the user to enter 5 test scores (the program must
store the scores into an array named test_scores);
- calculates the average score (the program must use the
array test_scores);
- displays the results.
Array Practice
1. Greet the user
2. Create a place where 5 test scores can be recorded (initialize to 0)
3. Create a place where the total of 5 test scores can be recorded and retained
temporarily; store 0 there initially
4. Create a place where the average of the 5 test scores can be recorded and retained
temporarily, store 0 there initially
5a. Prompt user to enter a test score
5b. Read user's input for a test score
5c. Repeat steps 5a and 5b until 5 scores are obtained
6a. Go to the place where the 5 test scores are recorded
6b. Read one test score
6c. Add the test score to total
6d. Repeat steps 6a through 6c until all 5 test scores and been read and added to total
7. Find average using formula: average = total / number of test scores
8. Display results to the user
9. End
#include <stdio.h> //My file 12.c
#define SIZE 5
int main (void)
{
int i = 0 ;
float test_scores [SIZE] = {0.0} , total = 0.0 , average = 0.0 ;
for ( i = 0 ; i <= SIZE ; i = i + 1)
{
printf("\n Enter a test score: ");
scanf("%f", &test_scores [i] );
}
for ( i = 0 ; i <= SIZE ; i = i + 1)
total = total + test_scores[i] ;
average = total / SIZE;
printf ("\n\n The average of your test scores is: %f \n\n\n", average);
return (0) ;
}
• Does this produce a correct result? Why or why not?
Assigning Values to Array Elements
• Cannot assign values to entire arrays as a whole
• Values are assigned to individual elements
– Examples:
golf_score[0] = 3;
test_2_grade[3] = 'C';
retail_price[2] = 1234.45;
Assigning Values to Array Elements
• More array element assignment examples:
retail_price[0] = 5.75;
retail_price[0] = retail_price[0] * 1.03;
retail_price[1] = retail_price[2] - 2;
retail_price[2] = retail_price[1] * 7;
retail_price[x] = retail_price [x + 1];
retail_price[x] = retail_price [x] + 1;
• Note: index can be any integer expression.
Accessing Array Elements
• Remember: array indices always, always, always start at 0
• Access a specific array element by using the array name
followed by the index in square brackets:
golf_score[0]
total = total + golf_score[17]
© Copyright 2026 Paperzz