Access by Value
Ournormal access to a variable is by
value:
int a, b, c;
a=3; b=4;
c=a*b;
printf("Value of c is %d\n",c);
Gibbons | GNG1101-15: 1
Address Operator
We can discover the address of a variable
by using the address operator &:
int a;
a=3;
printf("Value of a is %d.\n",a);
printf("Address of a is %u.\n",&a);
Addresses are unsigned integers.
Gibbons | GNG1101-15: 2
Functions
In a function we can pass by value or by
address.
If we pass by value, we can use that value
but we cannot change it:
printf("Value of a is %d.\n",a);
To be able to change a variable in a
function, we must pass by address:
scanf("%d",&a);
Gibbons | GNG1101-15: 3
Pointers
The C language allows us to store the
address in a variable called a pointer.
We can manipulate pointers in a limited way.
A pointer is defined with a data type, an
integer pointer points to integers, a float
pointer points to floats etc.
All pointers are unsigned integers.
Gibbons | GNG1101-15: 4
Pointer Declaration
Declare integer a and a pointer ptr to an
integer
– note the use of the asterisk
int a, *ptr;
declare a variable ptr
as a pointer
to an integer
Gibbons | GNG1101-15: 5
Precedence
() [] left to right
+ - ++ -- ! * & (type) right to left
* / % left to right
+ - left to right
>= left to right
== != left to right
&& left to right
|| left to right
= right to left
, left to right
Gibbons | GNG1101-15: 6
Pointer Assignment
Set ptr to point to a
ptr=&a;
Pointers cannot be assigned a constant
except for 0 or NULL (stdio.h), which
indicates that the pointer is not assigned:
ptr=NULL;
Good practice demands that all pointers be
initialized - to NULL if necessary.
Gibbons | GNG1101-15: 7
Assignment in Declaration
int a, *ptr=&a;
Assign the address
of variable a
to the variable ptr
which is a pointer
to an integer
Gibbons | GNG1101-15: 8
Clarification
*ptr is an integer: int *ptr;
We can read *ptr as:
“the value contained in the address ptr”
We can read &a as: “the address of a”
int a, b, *ptr;
ptr=&a; //ptr is the address of a
b=*ptr; //the value contained in the
// address ptr is assigned to b
Equivalent to b=a;
Gibbons | GNG1101-15: 9
Indirection
int a, b, *ptr;
ptr=&a;
*ptr=b;
The value of b is assigned to the variable
whose address is ptr.
Note: in both cases, ptr contains the
address of a after the assignment with b.
Using a we access the variable directly,
using ptr we access the variable indirectly:
indirection. Gibbons | GNG1101-15: 10
Valid Assignments
int a, b, *aptr, *bptr; //* needed for each ptr
bptr=NULL; //bptr unassigned
aptr=&a; //aptr assigned the address of a
bptr=aptr; //bptr assigned the address of a
*aptr=b; //a assigned the value of b
bptr=&b; //bptr assigned the address of b
a=*bptr; //a assigned the value of b
Invalid:
aptr=500; //Constant not NULL
a=bptr; //Variable cannot hold a pointer
Gibbons | GNG1101-15: 11
Curiouser and Curiouser
A pointer contains the address to a variable
which has a value. However, the pointer is
a variable which has its own address:
int v, *ptr; ptr=&v;
ptr v
260 264 264 79
address of ptr value of ptr address of v value of v
Gibbons | GNG1101-15: 12
Test Program
#include
void main()
{
int v, *ptr;
v=79; ptr=&v;
printf("Value of v is %d.\n",v);
printf("Address of v is %u.\n",&v);
printf("Value of ptr is %u.\n",ptr);
printf("Address of ptr is %u.\n",&ptr);
}
Gibbons | GNG1101-15: 13
An Exercise
If:
int v, *ptr=&v;
what do the following statements reference?
– *&v
– &ptr
– *&ptr
– &*ptr
Gibbons | GNG1101-15: 14
Receiving Data From Functions
We have already seen that we can get one
data value returned from a function by the
use of the return statement.
When we need to return with more
information, we must supply the function
with addresses in which to deposit the
data.
Gibbons | GNG1101-15: 15
Back to the Circle
– We had the function getr which returned an
integer value which reflected the sign of the radius
we had read. But getr used the global variable
radius to pass back the radius.
– Rewriting:
int getr(float *rptr)
{
printf("Input radius ");
scanf("%f", rptr);
if(*rptr
int getr(float*); //No name - says whatever
// name you give will be
// taken as the address
void main()
{
float radius; int i; //New radius valid in
// main only
i=getr(&radius); //&radius is the address
printf("Value of i is %d.\n", i);
printf("Value of r is %f.\n", radius);
} Gibbons | GNG1101-15: 18
Coming Around Again
#include
#define PI 3.14159
int getr(float*, float*);
void main()
{
float radius, area;
while(getr(&radius, &area)>0)
{
printf("Value of radius is %f.\n",radius);
printf("Value of area is %f.\n",area);
}
}
Gibbons | GNG1101-15: 19
Latest getr
int getr(float *rptr, float *aptr)
{
printf("Input radius ");
scanf("%f", rptr);
*aptr=PI*(*rptr)*(*rptr); //Notice ( )
if(*rptr<0.0) return -1;
else return 1;
}
Gibbons | GNG1101-15: 20