Title: Chapter 7' C Pointers
1Chapter 7. C Pointers
27.2 Pointer variable declarations and
initialization
- A pointer is a variable that contains a memory
address as its value - A variable directly references a value while a
pointer indirectly references a value
37.2 Pointer variable declarations and
initialization
- The following declaration declares a pointer and
an integer - int countPtr, count
- The variable countPtr is declared as type int
(a pointer pointing to an integer value) - Pointers can be declared to point to any type of
data
4countPtr
count
7
count directly references the value 7 countPtr
indirectly references the value 7
57.2 Pointer variable declarations and
initialization
- A pointer can be initialized as an address, a
NULL, or a 0. Using 0 is equivalent to using
NULL. However, NULL is preferred. - int ptr1NULL
- int ptr20
- int ptr3y / will be explained later /
67.3 Pointer operators
- The operator (address operator) returns the
address of a variable. For example, the following
statement assigns the address of y to yPtr - yPtry
77.3 Pointer operators
- The following statements results in the following
figure - int y5
- int yPtry
yPtr
y
5
87.3 Pointer operators
- The operator (dereferencing operator) returns
the value pointed by a pointer - For example (see the figure and statements in the
previous slide), the following statement will
output the value 5. - printf(d,yPtr)
97.3 Pointer operators
- The operators and are complements of one
another (see the program in the next slide) - Figure 7.5 shows the precedence of the operators
and
10(No Transcript)
11Simple exercise
- Use pointers to switch the values of two variable
127.4 Calling functions by reference
- In C, functions are generally called by value.
Nevertheless, pointers can be used to simulate
calling by reference - See the next slide for an example
13include ltstdio.hgt void aa(int , int) main()
int a10,b10 printf(Before calling aa, ad,
bd\n,a,b) aa(a,b) printf(After calling
aa, ad, bd\n,a,b) void aa(int x, int
y) x 2 y 2 return
what will be outputted by this program?
14aa
main
x
a
10
b
y
10
10
157.4 Calling functions by reference
- See Figures 7.6 through 7.9 for a more detailed
explanation
16(No Transcript)
17(No Transcript)
18(No Transcript)
19(No Transcript)
207.4 Calling functions by reference
- As mentioned in the previous chapter, passing an
array to a function results in calling by
reference. Actually, passing an array corresponds
to passing the address of the first array element - When passing an array, the notation and can
be used interchangeably (see the next slide)
21void aa(int , int) main() int a10 . .
. aa(a,10) . . . void aa(int x, int y)
. . .
void aa(int , int) main() int a10 . .
. aa(a,10) . . . void aa(int x, int y)
. . .
22Simple exercise
- Use pointers to switch the contents of two arrays
in a function other than main
237.6 Bubble sort using call by reference
- The program in the following slides is a bubble
sort using call by reference - The call by reference occurs in the function
swap, see these statements - int holdelement1Ptr
- element1Ptrelement2Ptr
- element2Ptrhold
24(No Transcript)
25(No Transcript)
267.6 Bubble sort using call by reference
- The prototype of the function swap is defined
inside the function bubbleSort because only
bubbleSort uses swap. This restricts proper call
27The sizeof operator (pp. 267-269)
- The sizeof operator returns the number of bytes
used by a variable, that of a data type, or that
of a constant - See the programs in the following slides for some
examples
28(No Transcript)
29(No Transcript)
30(No Transcript)
317.7 Pointer expressions and pointer arithmetic
- If a pointer points to an array element, pointer
expression and arithmetic are meaningful - Pointers can be increased, decreased, added by a
value, subtracted by a value, or subtracted by
another pointer
327.7 Pointer expressions and pointer arithmetic
- Example
- int vPtr, v1,2,3,4,5,6,7,8,9,10,i
- vPtrv / this statement is the same as
vPtrv0 / - for(i0ilt10vPtr,i)
- printf(d\n,vPtr) / what will be
outputted? ? - The vPtr advances the pointer to the next array
element. Therefore, vPtr in the above example
causes the value of vPtr to be increased by
sizeof(int)
33v0
v1
v2
v9
vPtr vPtr1 vPtr2
vPtr9
347.7 Pointer expressions and pointer arithmetic
- Example
- float vPtr, v10
- vPtrv / suppose that 7000 is assigned to
vPtr/ - vPtr2
- Suppose that sizeof(float) is 4. Then, what value
will be assigned to vPtr by the statement vPtr2?
357.7 Pointer expressions and pointer arithmetic
- Example
- float ptr1,ptr2,v10
- int i
- ptr1v3/ suppose ptr1 is set 700C/
- ptr2v1/ suppose ptr2 is set 7004/
- iptr2-ptr1 / i will be set the number of array
elements between the two pointers / - What will i gets in the last statement above?
36Simple exercise
- Use pointer arithmetic to assign the summation of
two arrays to the third array, element by element
377.7 Pointer expressions and pointer arithmetic
- A pointer can be assigned to another pointer in
case that they have the same type - Cast operators should be used to convert pointer
type for the assignment between different pointer
types. For example - ptr1(int)ptr2
387.7 Pointer expressions and pointer arithmetic
- void pointer can used in assignment
- Example
- void vptr1,vptr2
- int iptr1
- float fptr1
- vptr1vptr2
- vptr1iptr1
- fptr1vptr2
397.7 Pointer expressions and pointer arithmetic
- NOTE a void pointer cannot be dereferenced
because the compiler does not know what is the
type pointed by the pointer
407.8 The relationship between pointers and arrays
- An array name can be though as a constant pointer
(pointing to which array element?) - Equivalent expressions (suppose that b is an
integer array and bPtr is an integer pointer) - bPtrb bPtrb0
- b3 (bPtr3)
- b3 bPtr3
- b3 (b3)
417.8 The relationship between pointers and arrays
- If a pointer points to an array, the pointer can
be increased or decreased (what do the increment
and decrement mean?) - On the other hand, array name cannot be increased
or decreased because it is actually a constant
pointer. See the next slide for an example
42int ptr,a101,2,3,4,5 ptra ptr3 /what
does this statement mean?/ printf(d,ptr) a
3 / a compiler error occur here /
437.8 The relationship between pointers and arrays
- The programs in the next slides show the
reference of an array using different methods
44(No Transcript)
45(No Transcript)
46(No Transcript)
477.9 Arrays of pointers
- The same type of pointer can be structured as an
array - The following declaration results in the figure
in the next slide - const char suit4 Hearts, Diamonds,
Clubs, Spades
48(No Transcript)
49???
- Write a program to
- 1. input 10 different integers from the keyboard,
in which five integers are stored in array a and
five are in array b, and - 2. use a function other than main to place the
largest five numbers in array a and the others in
array b. Please use pointers in this function. - Due the day after 2 weeks