Engineering Problem Solving with C Fundamental Concepts - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Engineering Problem Solving with C Fundamental Concepts

Description:

Engineering Problem Solving with C Fundamental Concepts Chapter 6 Pointers – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 27
Provided by: SoniaC6
Category:

less

Transcript and Presenter's Notes

Title: Engineering Problem Solving with C Fundamental Concepts


1
Engineering Problem Solving with C Fundamental
Concepts
  • Chapter 6
  • Pointers

2
Addresses and Pointers
3
Address 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

4
Pointer 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)

5
How 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.)

6
Example
  • 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 been initialized in the
    above example
  • declaring a pointer creates a variable capable of
    holding an address

7
Example
  • int a, iPtrchar sdouble dPtr

a
?
iPtr s dPtr
?
-
?
?
8
More 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. q is declared to be an int.

9
Assigning 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

10
Example
  • example -
  • int I6, j
  • int iPtr
  • iPtr I
  • j iPtr

I
6
iPtr
6
j
11
Practice!
  • Give a memory snapshot after each set of
    assignment statements
  • int a1, b2, ptr
  • ptr b
  • int a1, b2, ptrb
  • a ptr

12
NULL 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
  • Is guaranteed to compare unequally with any
    pointer that is not a null pointer.

13
Example-
  • int iPtr0char s0double dPtrNULL

iPtr s dPtr
14
Pointer Assignment
  • 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
15
Pointer 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

16
Comparing 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.

17
Pointers 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)

18
More 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)

19
Two-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, sptrs00
  • Memory allocation
  • s00 2
  • s01 4
  • s02 6
  • s10 1
  • s11 5
  • s12 3
  • A pointer reference to s01 would be (sptr1)
  • A pointer reference to s11 would be (sptr4)
  • row offset number of columns column offset

20
Pointers in Function References
  • In C, function references are call-by-value
    except when an array name 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.

21
switch 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

22
Dynamic 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

23
Dynamic 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

24
malloc 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

25
Example 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(npts,sizeof(int))

Represents the number of bytes used to store a
double value
26
  • includeltstdio.hgt
  • includeltstdlib.hgt
  • int main()
  • int num_std,j
  • double ave0.0,grade
  • printf("How many students are there?\n")
  • scanf("d",num_std)
  • //declare malloc
  • grade(double)malloc(num_stdsizeof(double))
  • //declare calloc
  • //grade(double)calloc(num_std,sizeof(double))
Write a Comment
User Comments (0)
About PowerShow.com