Introduction to Programming in C - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Introduction to Programming in C

Description:

A pointer variable can be hold the address of an array element. Declare ... printf('not a palindrome!n'); break; if (p q) printf('Palindrome!n'); return 0; ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 14
Provided by: drjames82
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Programming in C


1
Chapter 12
  • Introduction to Programming in C

2
Pointer Arithmetic
  • A pointer variable can be hold the address of an
    array element. Declare the variables
  • int a8, p
  • And then set the pointer to the address of a0
  • p a0
  • Now we can store an integer value in a0 by
    doing either of the following
  • a0 5
  • p 5 / Both do the same thing /

p
a
0
0
0
0
5
0
0
0
0 1 2 3 4
5 6 7
3
Pointer Arithmetic
  • int p
  • p or (p) Value is p, but then p increments
  • (p) Value is p, but then p increments
  • p or (p) Increment p first, then value is
    p
  • p or (p) Increment p first, then value is
    p

4
Pointer Arithmetic
  • What does each of these do?
  • int sum 0
  • for (p a0 p lt a8 p)
  • sum p
  • q 15
  • (q)
  • printf(d,q)
  • q

q
p
a
49
16
9
1
0
36
25
4
0 1 2 3 4
5 6 7
5
Pointer Arithmetic
  • What does each of these do?
  • int sum 0
  • for (p a0 p lt a8 p)
  • sum p / sum 140 /
  • q 15 / a1 15, q a2 /
  • (q) / a2 5
  • printf(d,q) / q a3, print 9 /
  • q / a3 becomes 10 /

q
p
a
49
16
9
15
0
36
25
5
0 1 2 3 4
5 6 7
6
Pointers and Arrays
  • We can use an array name as a pointer. These do
    the same thing
  • p a0
  • p a
  • Note that the loop
  • for (p a0 p lt a8 p)
  • sum p / sum 140 /
  • Can be replaced by this
  • for (p a p lt a 8 p)
  • sum p

p
a
49
16
9
1
0
36
25
4
0 1 2 3 4
5 6 7
7
Pointer Arithmetic
  • What does this do?
  • int sum 0
  • p a
  • while (p lt a8)
  • sum p / sum 145 /

p
a
49
16
9
15
0
36
25
5
0 1 2 3 4
5 6 7
8
Pointer and Arrays - Homework
  • P. 235 1, 3, 8, 9,12 (mail code in for 12).

9
8
  • include ltstdio.hgt
  • define N 100 / Max length of string /
  • main()
  • char aN, p
  • printf("Enter a message ")
  • p a
  • scanf ("c", p)
  • while (p ! '\n')
  • p
  • scanf ("c", p)
  • while (p gt a)
  • printf("c", p--)
  • printf("\n")

10
12
  • define N 10
  • main()
  • float p
  • float identNN
  • int col, row 0
  • p ident00
  • p 1.0
  • for(p ident01 p lt identN-1N-1
    p)
  • row
  • if (row N1)
  • p 1
  • row 0
  • else
  • p 0

11
9
  • define N 100 / Max length of string /
  • main()
  • char aN, p, q
  • / What goes here?? /
  • return 0

12
Program 12
  • 9, P. 237

13
9
  • define N 100 / Max length of string /
  • main()
  • char aN, p, q
  • printf("Enter a message ")
  • p a
  • scanf ("c", p)
  • while (p ! '\n')
  • if (((p gt 'A')(p lt 'Z')) ((p gt
    'a')(p lt 'z'))) p
  • scanf ("c", p)
  • p--
  • q a
  • while (p gt q)
  • if (p q)
  • p--
Write a Comment
User Comments (0)
About PowerShow.com