Const Pointers, Type Casts and void - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Const Pointers, Type Casts and void

Description:

The compiler knows the type of any expression or variable ... into a small type simply truncates (preserving only the least significant bits) ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 14
Provided by: usersEc3
Category:

less

Transcript and Presenter's Notes

Title: Const Pointers, Type Casts and void


1
Const Pointers, Type Casts and void
2
Type Casting in C
  • C is a statically typed language
  • The compiler knows the type of any expression or
    variable
  • Sometimes, the programmer knows better what
    information is stored in a particular location
  • Type casting is a method of overriding the
    compilers objections to the way youre using a
    variable or expression.

3
Implicit Type Conversion
  • Any integer type can be converted (silently) to
    any other integer type.
  • Converting a signed small type to a large type
    sign extends the value to preserve the 2s
    complement
  • Converting a large type into a small type simply
    truncates (preserving only the least significant
    bits)
  • You may get warnings about possible loss of
    precision when converting from a large type to a
    small type
  • int x 100000
  • short c x // legal, but will lose bits

4
More Implicit Type Conversion
  • You can also convert between integers and
    floating point values without a type cast
  • Floating point values are truncated (the
    fractional part is dropped) when converting to
    integers.
  • You may or may not get a warning.
  • Keep in mind that you can lose bits going either
    way!

5
Rules for Arithmetic
  • Arithmetic is (usually) performed using either
    double, or int (never float, short, or char)
  • Smaller types are automatically promoted to
    larger types
  • The exception is long and long long which will
    not be demoted to int the compiler will
    perform arithmetic using the longer type.
  • If there either of the two operands for an
    arithmetic operator (e.g. ) are float, then
    the operation will be performed using double

6
Arithmetic Examples
  • float x 1 / 2 // sets x to 0.0
  • float y 1.0 / 2.0 // sets y to 0.5
  • float z 1 / 2.0 // sets z also to 0.5
  • char c A
  • char d c 1 // performed using int then
  • // truncated back to char
  • doit(d) // argument is passed as an int

7
Explicit Type Conversion
  • You can order the compiler when and where to do
    type conversions with type casts
  • The type cast does not tell the compiler how to
    convert
  • The conversion will be done according to same
    rules as for implicit conversion.
  • The syntax of a type cast is to provide the name
    of the new type inside parenthesis
  • float f 3.14159
  • int x (int) (f) 1 // convert f to int and
    then add 1

8
Examples
  • float x 3.5
  • float y (int) x // sets y to 3.0
  • int z (float) 1 / 2 10 // sets z to 5
  • // same as z 1.0 / 2 10

9
Pointer Casts
  • The value of a pointer is an address
  • An address is an address is an address
  • All addresses are stored in the same way whether
    its the address of an integer or the address of
    a floating point, etc.
  • There is no conversion performed when assigning
    from one pointer type to another
  • There may be a type cast required

10
void
  • The void type of pointer is address of nothing
    in particular
  • A void pointer can be assigned the address of
    any variable (without a type cast)
  • A void pointer cannot be assigned to any other
    type of pointer (without a type cast)

11
Casting Pointers
  • A type cast with pointers does not change the
    address in any way
  • int x // assume x is at address 1000
  • int p x // sets p to 1000
  • char q
  • q (char) (p) // sets q to 1000
  • q 42 // assigns just 8 bits inside x

12
Const Pointers
  • Frequently, a multi-person software development
    team will agree to use pointers to share
    information between functions
  • By giving someone else the address of my data
    Im giving them the potential to change my data.
  • In many cases, I dont want them to change my
    data, I just want them to be able to access
    (i.e., read) my data.
  • const is an annotation that can be added to a
    pointer to indicate that the data should not be
    changed

13
Example
  • void strcpy(char dest, const char src)
  • Indicates that the data pointed to by dest can be
    changed, but the data pointed to by src should
    not be changed
Write a Comment
User Comments (0)
About PowerShow.com