Title: Engineering Problem Solving with C Fundamental Concepts
1Engineering Problem Solving with C Fundamental
Concepts
2Addresses and Pointers
3Address Operator
- A variable can be referenced using the address
operator - example
- scanf(f, x)
- This statement specifies that the value read is
to be stored at the address of x
4Pointer Assignment
- A pointer is a variable that holds the address of
a memory location - If a variable p holds the address of another
variable q, then p is said to point to q - If q is a variable at location 100 in memory,
then p would have the value 100 (qs address)
5How to declare a pointer variable
- pointer variables are declared using an asterisk
( ) The asterisk is called the indirection
operator or the de-referencing operator). - example
- int a, b, ptr
- ptr is a pointer to an integer
- when a pointer is defined, the type of variable
to which it will point must be specified. (i.e.
a pointer defined to point to an integer cannot
also point to a floating point variable.)
6Example
- int iPtr
- double dPtr
- the variable iPtr is declared to point to an int
- the variable dPtr is declared to point to a
double - neither variable has not been initialized in the
above example - declaring a pointer creates a variable capable of
holding an address
7Example
int a, iPtrchar sdouble dPtr
a
?
iPtr s dPtr
?
-
?
?
8More about declaring pointers
- When using the form
- int p, q
- the operator does not distribute.
- In the above example p is declared to be a
pointer to int. p is declared to be an int.
9Assigning values to a pointer
- the assignment operator () is defined for
pointers - the right operand can be any expression that
evaluates to the same type as the left - the operator in front of an ordinary variable
produces the address of that variable. The
operator is called to address of operator
10Example
- example -
- int I6, j
- int iPtr
- iPtr I
- j iPtr
- I iPtr
- j
-
6
6
11Practice!
Give a memory snapshot after each set of
assignment statements int a1, b2, ptr ptr
b int a1, b2, ptrb a ptr
12NULL pointer
- A pointer can be assigned or compared to the
integer zero, or, equivalently, to the symbolic
constant NULL, which is defined in ltstdio.hgt. - A pointer variable whose value is NULL is not
pointing to anything that can be accessed
13Example-
int iPtr0char s0double dPtrNULL
iPtr s dPtr
14Pointer Assignments
- A pointer can point to only one location at a
time, but several pointers can point to the same
location. - Example
- / Declare and initialize variables. /
- int x-5, y 8, ptr1, ptr2
- / Assign both pointers to point to x. /
- ptr1 x
- ptr2 ptr1
- The memory snapshot after these statements are
executed is - ptr1 ptr2
-
-
- x y
-5
8
15Pointer Arithmetic
- Four arithmetic operations are supported
- , -, , --
- only integers may be used in these operations
- Arithmetic is performed relative to the variable
type being pointed to - Example p
- if p is defined as int p, p will be incremented
by 4 (system dependent) - if p is defined as double p, p will be
incremented by 8(system dependent - when applied to pointers, means increment
pointer to point to next value in memory
16Comparing Pointers
- You may compare pointers using relational
operators - Common comparisons are
- check for null pointer (p NULL)
- check if two pointers are pointing to the same
object - (p q) Is this equivalent to
- (p q)
- compare two pointers that are pointing to a
common object such as an array.
17Pointers and Arrays
- The name of an array is the address of the first
elements (i.e. a pointer to the first element) - The array name is a constant that always points
to the first element of the array and its value
can not be changed. - Array names and pointers may often be used
interchangeably. - Example
- int num4 1,2,3,4, p
- p num
- / above assignment is the same as p num0
/ - printf(i, p)
- p
- printf(i, p)
18More Pointers and Arrays
- You can also index a pointer using array notation
- Example
- char string This is a string
- char str
- int i
- str string
- for(i 0 stri i) //look for null
- printf(c, stri)
19Two-Dimensional Arrays
- A two-dimensional array is stored in sequential
memory locations, in row order.
Array definition int s23 2,4,6,
1,5,3, sptrs
Memory allocation s00 2
s01 4 s02 6 s10 1 s11 5 s1
2 3 A pointer reference to s01 would be
(sptr1) A pointer reference to s11 would be
(sptr4) row offset number of columns
column offset
20Pointers in Function References
- In C, function references are call-by-value
except when an array name is is used as an
argument. - An array name is the address of the first element
- Values in an array can be modified by statements
within a function - To modify a function argument, a pointer to the
argument must be passed - The actual parameter that corresponds to a
pointer argument must be an address or pointer.
21switch Example
void switch2(int a, int b) / Declare
Variables. / int temp / Switch values
pointed to by a and b. / temp
a ab btemp / Void return.
/ return
22Dynamic Memory Allocation
- Dynamically allocated memory is determined at
runtime - A program may create as many or as few variables
as required, offering greater flexibility - Dynamic allocation is often used to support data
structures such as stacks, queues, linked lists
and binary trees. - Dynamic memory is finite
- Dynamically allocated memory may be freed during
execution
23Dynamic Memory Allocation
- Memory is allocated using the
- malloc function (memory allocation)
- calloc function (cleared memory allocation)
- Memory is released using the
- free function
- The size of memory requested by malloc or calloc
can be changed using the - realloc function
24malloc and calloc
- Both functions return a pointer to the newly
allocated memory - If memory can not be allocated, the value
returned will be a NULL value - The pointer returned by these functions is
declared to be a void pointer - A cast operator should be used with the returned
pointer value to coerce it to the proper pointer
type
25Example of malloc and calloc
int npts 500 double x int p / Allocate
memory for 500 doubles. / x (double
)malloc(nptssizeof(double)) / Allocate
memory for 500 integers. / p (int
)calloc(nptssizeof(int))
26Common Pointer Problems
- Using un-initialized pointers
- int iPtr
- iPtr 100
- iPtr has not been initialized. The value 100
will be assigned to some memory location,
resulting in various types of errors. - Failing to reset a pointer after changing its
value - Incorrect/unintended syntax