Strings as Arrays of Characters - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Strings as Arrays of Characters

Description:

char *Greeting = 'hello'; pointers. imagine passing the following ... string x = 'hello'; is passed around and used as a complete value. a string defined as: ... – PowerPoint PPT presentation

Number of Views:84
Avg rating:3.0/5.0
Slides: 14
Provided by: msresearch
Category:

less

Transcript and Presenter's Notes

Title: Strings as Arrays of Characters


1
Strings as Arrays of Characters
  • string Greeting "hello"
  • vs.
  • char Greeting "hello"

2
pointers
  • imagine passing the following string to a
    function
  • void printStr( string x )
  • x "Four score and seven years ago our fathers
    brought forth on this continent a new nation,
    conceived in Liberty, and dedicated to the
    proposition that all men are created equal. Now
    we are engaged in a great civil war, testing
    whether that nation, or any nation, so conceived
    and so dedicated, can long endure. We are met on
    a great battle-field of that war. We have come to
    dedicate a portion of that field, as a final
    resting place for those who here gave their lives
    that that nation might live. It is altogether
    fitting and proper that we should do this. But,
    in a larger sense, we can not dedicatewe can not
    consecratewe can not hallowthis ground. The
    brave men, living and dead, who struggled here,
    have consecrated it, far above our poor power to
    add or detract. The world will little note, nor
    long remember what we say here, but it can never
    forget what they did here. It is for us the
    living, rather, to be dedicated here to the
    unfinished work which they who fought here have
    thus far so nobly advanced. It is rather for us
    to be here dedicated to the great task remaining
    before us that from these honored dead we take
    increased devotion to that cause for which they
    gave the last full measure of devotion that we
    here highly resolve that these dead shall not
    have died in vain that this nation, under God,
    shall have a new birth of freedom and that
    government of the people, by the people, for the
    people, shall not perish from the earth."
  • printStr( x )

3
in computer memory
four
score
and
seven
years
ago
our

shall
not
perish
from
the
earth.
0
4
computer memory is ADDRESSED
100
four
104
score
109
and
112
seven
117
years
address
data
5
a shortcut
  • we could pass our huge string "x" to any function
    simply by passing the address of the first
    character.
  • the address of the first character is referenced
    like this " x ".
  • If x is defined as x
  • the x is a pointer to a string of characters.

6
value vs. reference
  • a string defined as
  • string x "hello" is passed around and used
    as a complete value.
  • a string defined as
  • char x "hello" is passed around and used as
    a reference, or pointer, to a string.
  • much quicker

7
note x
  • x refers to the actual address in memory of x
    in our example, x 100
  • if a function expects a pointer, and all you have
    is a variable, sometimes you can send the address
    and it won't complain
  • why? because a pointer IS an address.

8
we will use references where appropriate
  • strcmp, strcpy, strcat library functions only
    take pointers as arguments
  • char x "hawk"
  • char y "eagle"
  • strcmp( x, y ) yields a 1. why
  • -1 if hawk comes before eagle (it doesn't)
  • 0 if they're identical strings (they're not)
  • 1 if hawk comes after eagle (it does)

9
let's alphabetize a list
  • this may take a while
  • const int arraySize 8
  • char birds arraySize "eagles", "hawks",
    "bluejays", "robins", "cardinals", "owls",
    "doves", "ducks"

10
our menu of choices
11
we'll need to print the list
the call from main
12
Why is printAnyArray passed an array, rather than
just using the global birds array? - it is
more useful then, because it can print any array,
not just birds
birds is defined as a pointer
a pointer to an array is expected
13
we'll need to swap around array elements
Write a Comment
User Comments (0)
About PowerShow.com