• Classrooms and Classes

    You are already familiar with how classrooms and classes work:

    • classrooms hold classes
    • classrooms are permanent fixtures
    • classrooms can hold different classes at different times

    Now imagine all of University Of Hawaii's classrooms side by side like this:

  • Same Thing With Computer Memory

    Classrooms are to addresses as classes are to values:

  • Addresses

    What are addresses?

    • location of computer memory
    • it's an overly simplified explanation, but it's all you need to know for programming

    Why are addresses important for programming?

    • the C programming language allows the programmer the flexibility to manage memory
    • having a high level of control over memory allows C programs to be more efficient and take up less memory
    • other program languages take care of some memory issues for programmers
  • Values

    What are values?

    • data
    • could be real number, integer, or character
    • things you have been storing in your variables

    Why are values important for programming?

    • programs ultimately have to interact with some sort of value
    • values are important to humans
    • processing values (data) are what makes programs important
  • Addresses vs Values

    What are values and addresses?

    • values are things like:
      • 1 (integer)
      • 1.1 (real number)
      • a (character)
    • addresses are things like:
      • 0x7fff68db4100 (hexidecimal number)

    Why are values and addresses needed?

    • Values are used by humans
      • though a value like "1" is still kept in computer memory as some other type of machine readable concept (bit, byte, hexidecimal, etc)
    • Addresses are used by computers
      • addresses are represented in this way because computers work with binary values (i.e. 0/1, true/false, or on/off)

    Analogy of Addresses and Values

    Analogy of Addresses and Values
    ConceptProgramming WorldReal World
    Address 0x7fff68db4100 MSB114, 2500 Campus Rd, Honolulu, HI 96822
    Value 1.234 EE160 Class

    Things to note:

    • addresses are physical locations that should be treated as not changing, like classroom MSB114
    • values are transient data that can be modified, like MSB114 can house EE160 now and then later house another class like MATH101
  • Reference and Dereference Operators

    What are the reference and dereference operators?

  • Example of Pointers, Reference (Address Of) Operator, and De-Reference (Value Pointed By) Operator

    Examine pointer.c:

    output of pointer.c:

    ===integer===
                       1: integer, value of 'integer' variable
          0x7fff81d4c12c: &integer, address of 'integer' variable
                        : *integer is a syntax error
    
    ===integer_pointer===
          0x7fff81d4c12c: integer_pointer, value of 'integer_pointer' variable
                       1: *integer_pointer, value of the object that the 'integer_pointer' variable points to
          0x7fff81d4c120: &integer_pointer, address of 'integer_pointer' variable

    Step through the first part of the code (setting the information):

    In the end, you have something like this:

    Step through last part of the code (getting the information):

    Pointer and Detour Analogy:

    Takeaway:

    • Different Ways of Getting Values and Addresses
      To Get The ValueTo Get The Address
      using the variable integer integer &integer
      using the pointer integer_pointer *integer_pointer integer_pointer
    • the value of the variable (integer) is the same as the dereferenced pointer (*integer_pointer)
    • the value of the pointer (integer_pointer) is the same as the address of the variable (&integer)

    Things to note:

    • the pointer conversion specifier is %p
    • the * and & can be stacked like *(&integer)
    • the most likely pointer use cases are:
        for ints:
      • initialize pointer: int *integer_pointer
      • set to the address of a variable integer_pointer = &integer
      • get the value of the variable that the pointer is pointing to *integer_pointer
  • Assignment

    Homework Problem 5.1, at the end of this problem you should submit to Laulima:

    Homework Problem 5.2, at the end of this problem you should submit to Laulima:

    • a file named UHusername_05_2.c

      example:

      • zhaol_05_2.c
    • Write a program (based off of homework problem 5.1) that takes in an integer, decimal, and character and then prints them out
    • the program shall display the decimal value out to 5 decimal places by using the printf conversion specifier
    • the program shall accept only the first 6 digits of the integer entered by using the scanf conversion specifier
    • test case1:
      Please enter a character, a decimal, and an integer:
      [user enters b 1.23456 123456]
      character: b
      decimal: 1.23456
      integer: 123456
    • test case2:
      Please enter a character, a decimal, and an integer:
      [user enters z]
      [user enters 12345.6]
      [user enters 123456789]
      character: z
      decimal: 12345.60000
      integer: 123456