Title: CPS 196 Introduction to Computer Programming: C
1CPS 196Introduction to Computer Programming C
addresses pointers examples
2Overview
- Addresses of variables
- Basic types Pass-by-value
- Arrays Pass-by-reference
- Pointers
- scanf
- a function that modifies an array
- a function that modifies an int
- more examples
3Addresses of Variables
- Variables allocate space in memory.
- Each memory location has an address.
- Different variables will have different addresses.
4Primitive (Basic) Data Types Pass-By-Value
- For basic data types such as int, float, char,
double, only the value is sent to a function. - This is called "pass-by-value".
- The function uses a local variable to hold this
value. - Caller can't access this local variable.
- If the function changes the value, the caller
will never know.
5Arrays Pass-By-Reference
- Arrays are "passed-by-reference".
- The function shares the array sent it doesn't
make a copy of the array for itself. - Any change in the array is made directly in the
caller's array. - All changes will immediately and permanently
affect the caller's array.
6Pointers
- Pointers hold memory addresses as their values.
- The address of a pointer variable and the address
value it holds are different things. - The type of the pointer variable, and the type of
the thing it is pointing to are also different. - C uses pointers everywhere.
7Pointer Example
- n an int (integer) variable
- p an int (integer pointer pointer to integer)
variable.
int n 5 int p n
5
n
int
integer
0x0F00
0x0F00
integer pointer pointer to integer
0x0F04
p
int
8Adress-of Operator and Indirection Operator
- The address-of operator gives the address of a
variable (or array element) - int p
- p n // address of n an int
- Address is really a pointer.
- The indirection operator gives back the thing
pointed to - int m p // m gets n an int
9Pointer Arithmetic
- Pointer's view of the memory is as if the memory
only contains the type of value that this pointer
points to (int, double, char, etc). - We can move to the next location or previous
location by using , -- - This will go to the memory location of
next/previous int, double, char, NOT the next
byte in memory.
10Pointer Arithmetic Example
- If sizeof(int) is 4 (each int takes four bytes of
memory) - Code Value of p
- p n 0x0F00 (address of n)
- p 0x0F04 (4 bytes)
- p 0x0F08 (4 bytes)
- p-- 0x0F04 (-4 bytes)
- p 2 0x0F0C (8 bytes)
- p p - 3 0x0F00 (-12 bytes)
11Pointer Arithmetic and Arrays
- The pointer arithmetic matches with array access.
- This is actually how C handles arrays.
- An int array is a non-modifiable int (integer
pointer). - The elements of the array will be accessed using
pointer arithmetic.
12Pointer Arithmetic and Arrays An Example
int ar3 1, 4, 9 int p ar / Now,
these are equivalent ar0 p addr
of ar0 (an int) ar1 p 1 addr
of ar1 (an int) ar2 p 2 addr
of ar2 (an int) ar0 p
ar0 (an int) ar1 (p 1)
ar1 (an int) ar2 (p 2)
ar2 (an int) /
13How scanf Works
- We have seen operator in scanf
- scanf("d", you) / you's address /
- This sends the address of you to the scanf
function. - This address is an integer pointer.
- scanf can find you and change you now, because...
It knows where you live !!
14A Function Without Pointers
- This function does not work
void reset(int n) n 0
- Here, n is a local variable in the reset
function. - Changing it does not affect the caller.
15Same Function With Pointers
void reset(int p) p 0
- This function needs the address of an integer
variable, or an integer pointer value. - This affects the caller's variable.
16Functions With and Without Pointers
- These functions are called differently
void reset(int n) n 0 int main() int
n 5 reset(n) / n is still 5 /
printf("d", n) return n
void reset(int p) p 0 int main()
int n 5 reset(n) / n is now 0 /
printf("d", n) return n
5
0
17Array Addresses and Pointers
- Array, sent to a function is also handled with a
pointer. - Instead of int, you could use int, and your
function will work exactly the same way. - You can send pointer values to a function that
takes an array. - You can send an array to a function that takes a
pointer.
18Array Addresses and Pointers A Warning
- Warning If a function tries to access many
"array elements" and you just send the address of
a single variable, the function may access memory
locations that don't hold variables!
n
0x0F00
?
0x0F04
?
0x0F08
?
0x0F0C
?
0x0F10
19Array Addresses and Pointers A Warning An
Example
void reset3(int ar) ar0 ar1 ar2
0 int main() int k 5 reset3(k)
// bad idea!! return 0
0
k
0xF200
0
?
0xF204
0
?
0xF208
- This buggy program accesses memory locations that
are not defined. - It may crash!
20Pointer Examples 1
int k 5 int p p k p 0 // k is now
0 p 3 // k is now 3 p p 2 // k is
5 p 4 // k is 20 p 5 // k is
25
double a 5.5 double dp dp a dp 0.5
// a is now 0.5 dp 3.3 // a is now
3.3 dp p 2.25 // a is 5.55 dp 4
// a is 22.2 dp 5.8 // a is 28.0
char ch 'A' char cp cp ch cp 'Z' //
ch is 'Z' cp - 2 // ch is 'X' cp ' ' //
ch is ' '
21Pointer Examples 2
int j 3 int k 5 int p // no init val p
j // p is j p -p // j is now -3 p
k // p is k p 0 // k is now 0 // (j,
k) (-3, 0)
char hi3 'h', 'i', '\0' char cp hi
// cp is hi0 printf("c", cp) // 'h' cp
// cp is hi1 printf("c", cp)
// 'i' cp // cp is hi2 if
(cp '\0') // true printf("!\n") //
"!\n"
22Pointer Examples 3
char hi3 'h', 'i', '\0' char cp hi
// cp is hi0 while(cp ! '\0')
printf("c", cp) cp printf("\n") //
"\n"
char hi3 'h', 'i', '\0' char cp hi
// cp is hi0 while(cp ! '\0') //
till '\0' char printf("c", cp) // print
char go 1 printf("\n") // "\n"
h
hi
hi
23Pointer Examples Errors
double d double dp int n int p p 0 p
n n p 5 p 5 p 2.5 dp d dp
2.5 p d d dp 5 p 25 dp d
Error p not initialized (points to random memory
location) OK. p now points to n Error p points
to n, p is n, but n was not initialized! OK.
now, n is initialized, to 5 Error p points to
int p is int (but 2.5 is not) OK. dp now
points to d OK. d now has the value 2.5 Error p
is int, not double Error (modulus) operator
only works with ints (dp is double) Error p is
int (address), not an int Error dp is double,
not a double
24Pointer Examples Functions
- Functions can only return one value.
- You can also return values using pointer
arguments (parameters).
char getch() return getchar() int main()
char ch getch() printf("c", ch)
printf("c", getch()) return 0
void setch(char cp) cp getchar() int
main() char ch setch(ch)
printf("c", ch) return 0
25Pointer Examples Functions
- Here is a function that sets two values
/ gets next char into cp. If it is a digit,
also sets p, to that digit's integer value
/ void getdigit(char cp, int p) cp
getchar() if (cp gt '0' cp lt '9')
p cp - '0' int main() char ch
int digit -1 getdigit(ch, digit)
printf("c --gt d", ch, digit) return 0
26Pointer Examples Functions
- Here is a different version of the same function
/ gets and returns next char. If it is a
digit, also sets p, to that digit's integer
value / char getdigit2(int p) char c
getchar() if (cp gt '0' cp lt '9')
p cp - '0' return c int main()
char ch int digit -1 ch
getdigit(digit) printf("c --gt d", ch,
digit) return 0
27Pointer Examples Functions
- Does this version run the same way?
/ gets next char into cp. If it is a digit,
also returns that digit's integer value / int
getdigit(char cp) int dgt cp
getchar() if (cp gt '0' cp lt '9')
dgt cp - '0' return dgt int main()
char ch int digit -1 digit
getdigit(ch) printf("c --gt d", ch,
digit) return 0
No! There is a bug. If character is not a
digit dgt is never initialized!! (it will have a
random value)