Compound Types: References, Pointers - PowerPoint PPT Presentation

About This Presentation
Title:

Compound Types: References, Pointers

Description:

Arrays, Vectors, and Strings Allocation and referencing – PowerPoint PPT presentation

Number of Views:109
Avg rating:3.0/5.0
Slides: 19
Provided by: nemo68
Learn more at: https://www.cise.ufl.edu
Category:

less

Transcript and Presenter's Notes

Title: Compound Types: References, Pointers


1
Arrays, Vectors, and Strings
Allocation and referencing
2
Array Type
  • Arrays are a compound type
  • Base_type namesize
  • Array dimension specifies the number of elements
  • Dimension must be a positive integer literal OR
    a constant expression (i.e., known at compile
    time)

3
Array Examples
cnt 100 // not a const expr constexpr max
100 // a const expr int array1100 // good int
array2max // good int array3cnt // error
not const int array4getsize(something) //
good iff getsize() is constexpr
4
Arrays
  • Array indices run from 0 to Max-1
  • Always check your index!!
  • Can reference element by index or by pointer
    dereference
  • Ai or (A i)
  • Type allows compiler to compute correct offset
    into memory

5
Arrays
Address
RAM
int array151,2,4,8,16 char
name5Mary int ptr array10
0xfa58
0xfa54
0xfa50
0xfa4c
0xfa48
0xfa44
0xfa40
Note null terminated
0xfa3c
Symbol Table
0xfa38
0xfa34
0xfa30
\0 \0 \0 \0
Identifier
Type
Location
0xfa2c
y a r M
array1
int
0xfa18
0xfa28
0x00000010
0xfa24
0x00000008
name
char
0xfa2c
0x00000004
0xfa20
0xfa1c
0x00000002
ptr
int
0xfa18
0xfa18
0x00000001
0xfa14
0xfa10
6
2-D Arrays
  • C does not really have multi-D arrays
  • Looks kind of like it int AMN
  • Arrays are really pointers to the first element
    in a consecutively stored sequence of elements of
    same type
  • 2-D array is really pointer to a 1-D array of
    pointers to first row elements

7
C-Style Strings
  • C has a String class
  • Can be referenced by index like array
  • But it is a true object
  • C strings are not a primitive type, nor are they
    a struct
  • A C-style string is just a 1-D array of char,
    with NULL termination
  • NOTA BENE always a '\0' at end!!!!

8
2-D Arrays
Address
RAM
char names3Julian, James,John
0xfa58
0xfa54
? ? \0 n
0xfa50
0xfa4c
h o J \0
0xfa48
s e m a
0xfa44
J \0 n a
0xfa40
i l u J
0xfa3c
0x0000fa4d
Symbol Table
0xfa38
0x0000fa47
0xfa34
0x00000fa40
0xfa30
\0 \0 \0 \0
Identifier
Type
Location
0xfa2c
y a r M
array1
int
0xfa18
0xfa28
0x00000010
0xfa24
0x00000008
name
char
0xfa2c
0x00000004
0xfa20
0xfa1c
0x00000002
ptr
int
0xfa18
0xfa18
0x00000001
names
char
0xfa34
0xfa14
0xfa10
9
C-Style Strings and Chars
  • Remember, char and string are not the same
  • 'a' is a char literal uses one byte of RAM
  • a is a string literal uses two bytes of RAM
  • Name Joe - uses... 4 bytes for the 3
    characters plus null character
  • Char name allocate pointer ONLY!!
  • ltstrings.hgt library many functions

10
C String Library
include ltstrings.hgt istrlen(p) // string
length less null istrcmp(p1,p2) // neg if
p1ltp2, etc. p1strcat(p1,p2) // appends p2 to
p1 p1strcpy(p1,p2) // copies p2 to p1 /
WARNING NO BOUNDS CHECKING! / / use these
safe versions below! / istrncmp(p1,p2,n) //
only up to n p1strncat(p1,p2,n) // only up to
n p1strncpy(p1,p2,n) // only up to n
11
C String Library
include ltstrings.hgt size_t strlen(char s) //
not int! int atoi(char s) // int value of
s double atof(char s) // double value long
atol(char s) // long value void itoa(int val,
char s, int radix) // converts integer value
to string
12
C Strings
  • C has string type
  • include ltstringgt
  • using stdstring
  • string s1 // default to empty
  • string s2s1 // s2 is a copy of s1
  • string s3Joe // s3 is copy of literal
  • string s4(10,c) // s4 is cccccccccc
  • while (cin gtgt word) // whitespace delim
  • while (getline(cin, line)) // \n delim

13
Strings C-Strings
string s(Hello World) char strs //
error! const char str s.c_str() // OK /
Achtung! Contents of str may change Copy into
local version if need continued access to
contents / Use s.insert(), s.erase(),
s.assign(), s.append(), and s.replace() on
strings
14
Vectors
  • Vectors very similar to arrays
  • Except they are class templates, not a type
    must include type in declaration
  • Take care of memory management
  • Use push_back() to expand

15
Vector Initialization
vectorltdoublegt dvec // ivec empty vectorltintgt
ivec(10, 5) / 10 ints all 10 have value 5
/ vectorltintgt ivec2(ivec) // copy
ivec vectorltintgt ivec3 ivec // also
copy vectorltTgt vec4(10) / 10 item vector of
type T objs default init / vectorltintgt
ivec52,3,5,7,11,13 // 6 elements with values
given vectorltintgt ivec6 1,2,4,8,16 // 5
elements with values given
16
Vector Initialization
vectorltintgt ivec1(10) vectorltintgt
ivec210 vectorltintgt ivec3(10,
5) vectorltintgt ivec410, 5 vectorltstringgt
sv1Al,Mo, Jo vectorltstringgt
sv2(Al,Mo,Jo)
// 10 ints all value 0
// one int value 10
// 10 ints all value 5
// 2 ints, values 10 5
// list initialization
// error
17
Adding to a Vector
  • Vector size may not be known
  • Vector may be large
  • Use push_back member function

vectorltintgt iv for (int i0 i ! MAX i)
iv.push_back(ii)
vectorltstringgt text while (cin gtgt word) // word
is string text.push_back(word)
Subscripting does not add elements!
18
Range For
  • Auto type definition if unknown
  • Range for like foreach

vectorltintgt iv21,2,3,4,5,6,7,8,9 for (auto
i0 iv2) // reference! i i // square
elements for (auto i iv2) // non-reference
cout ltlt i ltlt endl
Write a Comment
User Comments (0)
About PowerShow.com