Arrays, Pointers, and Pointer Arithmetic - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Arrays, Pointers, and Pointer Arithmetic

Description:

Or, in other words, at time allocation of memory. ... These two operation are assignment, NOT initialization. Dr. M. Moussavi. 11 ... – PowerPoint PPT presentation

Number of Views:145
Avg rating:3.0/5.0
Slides: 14
Provided by: mous1
Category:

less

Transcript and Presenter's Notes

Title: Arrays, Pointers, and Pointer Arithmetic


1
Arrays, Pointers, and Pointer Arithmetic
2
Arrays and Pointers
  • The name of an array is treated as a constant
    pointer that points to the first element of the
    array. Therefore the array name and pointers of
    the same type have some similarities
  • int a 6 4,2,3,1,8, 11
  • cout ltlt a // Using Pointer Notation
  • cout ltlt a0 // Using Array Notation
  • Both statements above print 4

3
Arrays and Pointers
  • More Examples
  • int a6 4,2,3,1,8, 11
  • int b
  • ba
  • cout ltlt b //displays 4
  • cout ltlta // displays 4
  • cout ltlt b0 // displays 4
  • cout ltlt b1 // displays 2

0 1 2
3 4 5
a
b
1000
1004
1008
1012
1016
1020
4
Arrays and Pointers
  • In other words the array notations and pointer
    notations are interchangeable.
  • What are the the following statements output?
  • cout ltlt (b3)
  • cout ltlt a 2
  • cout ltlt (a1)
  • cout ltlt a
  • cout ltlt a 2

// Output is 1
// Output is the address of third element
// Output is 2
// Output is the address of first element
// Output is 6 (42)
0 1 2
3 4 5
a
b
1000
1004
1008
1012
1016
1020
5
Passing Arrays and Strings to Functions
  • Since arrays name and string name are pointers,
    therefore an array or string can appear as a
    pointer in a function parameter list. The
    following function prototypes are equivalent.
  • void fun (int x)
  • void fun (int x)
  • void fun (char string)
  • void fun (char string)

6
Pointer Arithmetic
  • Legal pointer arithmetic in C
  • Pointer Integer
  • Pointer Integer
  • Pointer Pointer
  • Pointer, or Pointer
  • Pointer, or --Pointer
  • Other arithmetic operations are illegal. An
    operation like Integer Pointer is not also
    allowed.

7
Pointer Arithmetic
  • pointer n refers to the address of nth
    element , from the current address. In other
    words
  • pointer n current address n sizeof (type)
  • E.g
  • double d3 4.5, 6.9, 7.7
  • double ptr d0

ptr
1000
1008
1016
d
ptr ptr 2 // moves ptr to the address1000
28
ptr
d
1000
1008
1016
8
Pointer Arithmetic
  • Pointer1 Pointer2, results in an integer
    value that represents the number of elements
    between the two pointers
  • int a5 2, 6, 4, 7, 9
  • int ptr
  • ptr a2
  • int diff
  • diff ptr a
  • In this example the value of diff will be 2.

9
Example
  • int my_strlen (char string)
  • char p
  • p string
  • while (p ! \0)
  • p
  • return (p string)

int main () int length const char s
xyz length my_strlen ( s )
printf (The string length is\
d., length) return 0
stack
p
String constant area
AR my_strlen
string
x
y
s
s
z
AR main
length
\0
3
No args
10
Initialization vs. Assignment
  • In general, initialization of variables in C is
    only possible at the time of declaration of
    variables. Or, in other words, at time allocation
    of memory. For example, the following operations
    are called initialization
  • int a 20
  • int b 40
  • int c3 3, 6,1
  • Now, consider the following statement
  • int x
  • char y
  • x 9
  • y A

These two operation are assignment, NOT
initialization.
11
Initialization of Arrays
  • Consider the following initialization of array,
    some are valid, and some are invalid
  • int myarray26
  • int myarray16 2, 5, 7 //OK
  • int myarray3 3,4, 6, 8 // OK
  • int myarray4 3 4, 5, 6, 7 //ERROR, not
    enough space
  • char string32 ABCD // ERROR, not
    enough space
  • char string15 ABC // OK
  • char string2 XYZ // OK
  • char string3 KLH // OK, but
    bad style
  • const char string4 CBC // better
    style

12
Array Initialization
  • Difference between pointer and array
    initialization
  • char s Jack
  • char ptr Joe

s
J
2000
String Constant Area
a
2001
J
1000
c
2002
o
1001
k
2003
e
1002
\0
2004
\0
1003
ptr
2005
No Args
13
Pointer Initialization
  • The following statement are all known as
    initialization operations in C/C.
  • int a5, b a, cNULL
  • int d a
  • But the following statements are assignment
    operations
  • a 9
  • b a
  • c a
  • d a
  • d 44
Write a Comment
User Comments (0)
About PowerShow.com