Professional Documents
Culture Documents
EcE 22014, CH 7 All
EcE 22014, CH 7 All
C Pointers
Chapter 7
7.1 Introduction
7.2 Pointer Variable Definitions and Initialization
7.3 Pointer Operators
7.4 Passing Arguments to Functions by Reference
7.5 Using the const Qualifier with Pointers
7.6 Bubble Sort Using Call-by-Reference
7.7 Size of Operator
7.8 Pointer Expressions and Pointer Arithmetic
7.9 Relationship between Pointers and Arrays
7.10 Arrays of Pointers
Initialization
int*countPtr, count;
specifies that variable countPtr is of type int * (i.e., a pointer to an integer) and is
read, “countPtr is a pointer to int” or “countPtr points to an object of type int.”
The asterisk (*) notation used to declare pointer variables does not
distribute to all variable names in a declaration. Each pointer must be
declared with the * prefixed to the name; e.g., if you wish to declare xPtr
and yPtr as int pointers, use int *xPtr, *yPtr;.
Fig. 7.2 | Graphical representation of a pointer Variable yPtr is said to “points to” y
pointing to an integer variable in memory.
8
DEPARTMENT OF
ELECTRONIC
ENGINEERING
9
DEPARTMENT OF
ELECTRONIC
Common Programming Error 7.3 ENGINEERING
Reference
There are two ways to pass arguments to a function call-by-value
and call-by-reference. All arguments in C are passed by value.
• Call by Value
• Call by Reference
In call by value, original value cannot be
In call by reference, original value is changed or modified. In call by value, when you
changed or modified because we pass reference passed value to the function it is locally stored by the
(address). Here, address of the value is passed in the function parameter in stack memory location. If you
function, so actual and formal arguments shares the change the value of function parameter, it is changed
same address space. Hence, any value changed for the current function only but it not change the
inside the function, is reflected inside as well as value of variable inside the caller method such as
outside the function. main().
11/23/2023 Daw Khaing Zar Zar Myint 14
DEPARTMENT OF
ELECTRONIC
ENGINEERING
Only one value in a calling function can be altered when using call-by-value. That value
must be assigned from the return value of the function to a variable in the caller. To modify
multiple variables from a calling function in a called function, use call-by-reference.
The function processes the array string (pointed to by sPtr) one character at a
time using pointer arithmetic.
63 element2Ptr point */
64 void swap( int *element1Ptr, int *element2Ptr )
65 {
66 int hold = *element1Ptr;
67 *element1Ptr = *element2Ptr;
68 *element2Ptr = hold;
69 } /* end function swap */
• sizeof
Operator
• Returns size of operand in bytes
• For arrays: size of 1 element * number of elements
• if sizeof( int ) equals 4 bytes, then
• int myArray[ 10 ];
• printf( "%d", sizeof( myArray ) );
• will print 40
• sizeof can be used with
• Variable names
• Type name
• Constant values
Performance Tip 7.2
Sizeof is a compile-time operator, so it does not incur any execution-time overhead.
11/23/2023 Daw Khaing Zar Zar Myint 35
DEPARTMENT OF
ELECTRONIC
Portability Tip 7.2 ENGINEERING
The number of bytes used to store a particular data type may vary
between systems. When writing programs that depend on data type
sizes and that will run on several computer systems, use sizeof to
determine the number of bytes used to store the data types.
Arithmetic
Assume that array int v[5] has been defined and its first element is at
location 3000 in memory. Assume pointer vPtr has been initialized to point
to v[0]—i.e., the value of vPtr is 3000. Figure 7.18 illustrates this situation
for a machine with 4-byte integers. Variable vPtr can be initialized to point
to array v with either of the statements
vPtr = v;
vPtr =
&v[ 0 ];
vPtr += 2;
Using pointer arithmetic on a pointer that does not refer to an element in an array.
bPtr = b;
bPtr = &b[ 0 ];
*( bPtr + 3 )
&b[ 3 ]
bPtr + 3
*( b + 3 )
Pointers
Arrays may contain pointers. A common use of an array of pointers is to form
an array of strings, referred to simply as a string array.
Coin